提问者:小点点

如何在Windows上将python请求与Putty SOCKS代理一起使用?


我已经设置Putty来创建服务器的Socks代理。我可以使用Chrome和Firefox访问主机。当我使用请求时,我会得到一个错误,它似乎表明Putty的连接有问题。我尝试设置HTTP_PROXY env变量,并尝试传递proxies参数。env variable方法产生“未能解析”错误,proxies方法产生最大重试次数。

设置env会在调用requests.get("http://10.122.116.17:8080/")时产生此错误

requests.packages.urllib3.exceptions.LocationParseError: Failed to parse: Failed to parse: "localhost:1088"

调用请求时,传递代理参数会产生此错误。得到(”http://10.122.116.17:8080/“,代理=代理)

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会话。

有人对这里可能发生的事情有任何提示或建议吗?我一直在谷歌上搜索,直到我的手指受伤!

提前感谢!!!

以下是我使用Firefox时服务器的外观…

----------------指令输出-------------------------

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'
>>>


共1个答案

匿名用户

请求此时不支持SOCKS代理。我们希望在底层的urllib3库中获得支持,但没有明确的时间框架。这一期跟踪了它。

2016年编辑:从Requests 2.10开始,Requests现在支持SOCKS代理。您可以通过安装socksextra:pip安装请求[socks]获得支持。