【问题标题】:I tried to send a python request to a proxy but it failed我试图向代理发送 python 请求,但失败了
【发布时间】:2020-09-17 13:59:42
【问题描述】:

我尝试向代理发送 python 请求,但失败了。我做错了什么?

代码如下:

import requests
url = 'https://httpbin.org/ip'

proxies = {
    "http": 'http://103.250.153.242',
    "https": 'http://103.250.153.242'
}

response = requests.get(url,proxies=proxies)
print(response.json())

结果应该返回 {'origin': '103.250.153.242'} 但我得到了这个:

Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "C:\Python37\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection
    raise err
  File "C:\Python37\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我该如何解决这个问题?

【问题讨论】:

    标签: python python-requests proxies


    【解决方案1】:
    import urllib
    import requests
    
    r = requests.get('http://google.com', proxies=urllib.request.getproxies())
    

    但是如果你有代理列表:

    from urllib3 import ProxyManager, make_headers
    
    default_headers = make_headers(proxy_basic_auth='myusername:mypassword')
    http = ProxyManager("https://myproxy.com:8080/", headers=default_headers)
    
    # Now you can use `http` as you would a normal PoolManager
    r = http.request('GET', 'https://stackoverflow.com/')
    

    或者:

    import requests
    proxies = {
        'http': 'http://proxy.server:port',
        'https': 'http://proxyserver:port',
    }
    s = requests.Session()
    s.proxies = proxies
    r = s.get('https://api.ipify.org?format=json').json()
    

    【讨论】:

    • 使用第二种方法(带请求),您在哪里输入代理授权凭据?
    【解决方案2】:

    你有这些代理:

    proxies = {
      "http": 'http://103.250.153.242',
      "https": 'http://103.250.153.242'
    }
    

    但这不正确,因为IP没有端口号,只需添加您对这个ip的端口,没有其他端口。 它会是这样的:

    proxies = {
      "http": 'http://103.250.153.242:PORT',
      "https": 'http://103.250.153.242:PORT'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2020-05-27
      • 2020-04-06
      • 2016-02-22
      相关资源
      最近更新 更多