我已经设置了Putty来为服务器创建一个Socks代理。我可以使用Chrome和Firefox访问主机。 当我使用请求时,我得到一个错误,似乎表明与Putty的连接有问题。 我试图设置HTTP_PROXY env变量,我试图传递代理参数。 env变量方法产生“无法解析”错误,并且proxies方法产生超出的最大重试次数。
设置env会在调用requests.get("http://10.122.116.17:8080/")
requests.packages.urllib3.exceptions.LocationParseError: Failed to parse: Failed to parse: "localhost:1088"
传递proxies参数会在调用requests.get("http://10.122.116.17:8080/",proxies=proxies)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=1088): Max retries exceeded with url: http://10.122.116.17:8080/ (Caused by <class 'httplib.BadStatusLine'>: '')
我已安装Wireshark并在我的代理服务器上进行过滤,该服务器位于216.255.67.119。当我运行Firefox时,我看到此地址的流量。当我运行Python请求时,我没有看到任何流量到服务器。所以我怀疑请求中的代理机制没有进入我的Putty会话。
有没有人有任何提示或建议可能会发生在这里?我用Google搜索,直到我的手指受伤!
提前致谢!!!
这是我使用Firefox时服务器的样子......
---------------- cmd输出-------------------------
C:\Users\patman>set HTTP_PROXY="localhost:1088"
C:\Users\patman>echo %HTTP_PROXY%
"localhost:1088"
C:\Users\patman>set HTTPS_PROXY="localhost:1088"
C:\Users\patman>python
Enthought Python Distribution -- www.enthought.com
Version: 7.3-2 (64-bit)
Python 2.7.3 |EPD 7.3-2 (64-bit)| (default, Apr 12 2012, 15:20:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "credits", "demo" or "enthought" for more information.
>>> import requests
>>> requests.get("http://10.122.116.17:8080/")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\api.py",
line 55, in get
return request('get', url, **kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\api.py",
line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\sessions
.py", line 346, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\sessions
.py", line 449, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\adapters
.py", line 263, in send
conn = self.get_connection(request.url, proxies)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\adapters
.py", line 188, in get_connection
conn = ProxyManager(self.poolmanager.connection_from_url(proxy))
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\packages
\urllib3\poolmanager.py", line 122, in connection_from_url
u = parse_url(url)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\packages
\urllib3\util.py", line 156, in parse_url
raise LocationParseError("Failed to parse: %s" % url)
requests.packages.urllib3.exceptions.LocationParseError: Failed to parse: Failed to parse: "localhost:1088"
>>> >>> proxies = {
... "http": "localhost:1088",
... "https": "localhost:1088",
... }
>>> requests.get("http://10.122.116.17:8080/",proxies=proxies)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\api.py",
line 55, in get
return request('get', url, **kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\api.py",
line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\sessions
.py", line 346, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\sessions
.py", line 449, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests-1.2.0-py2.7.egg\requests\adapters
.py", line 318, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=1
088): Max retries exceeded with url: http://10.122.116.17:8080/ (Caused by <clas
s 'httplib.BadStatusLine'>: '')
>>> requests.__version__
'1.2.0'
>>> import urllib3
>>> urllib3.__version__
'1.7.1'
>>>
腻子设定......
答案 0 :(得分:2)
请求目前不支持SOCKS代理。我们希望在底层的urllib3
库中获得支持,但是没有明确的时间框架。它由this issue跟踪。
2016年编辑:自请求2.10起,请求现在支持SOCKS代理。您可以通过安装socks
额外pip install requests[socks]
来获得支持。