我尝试使用一个依赖于 Python 请求的包与 Python 2.7 shell 中的 API 进行交互。问题是远程地址被我的网络(大学图书馆)屏蔽了。
因此,对于 API,我做了以下工作:
~$ ssh -D 8080 name@myserver.com
然后,在新的终端,在本地计算机:
~$ export http_proxy=socks5://127.0.0.1:8080 https_proxy=socks5://127.0.0.1:8080
然后我在 Python 控制台中运行该程序,但失败了:
~$ python
>>> import myscript
>>> id = '1213'
>>> token = 'jd87jd9'
>>> connect(id,token)
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 518, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 585, in send
r = adapter.send(request, **kwargs)
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
conn = self.get_connection(request.url, proxies)
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 273, in get_connection
proxy_manager = self.proxy_manager_for(proxy)
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 169, in proxy_manager_for
**proxy_kwargs
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 43, in SOCKSProxyManager
raise InvalidSchema("Missing dependencies for SOCKS support.")
requests.exceptions.InvalidSchema: Missing dependencies for SOCKS support.
以下节选自 Py 请求模块:
> try:
> from .packages.urllib3.contrib.socks import SOCKSProxyManager except ImportError:
> def SOCKSProxyManager(*args, **kwargs):
> raise InvalidSchema("Missing dependencies for SOCKS support.")
现在问题似乎来源于 urllib3的 SOCKSProxyManager。
所以我读到你可以使用 SOCKSProxyManager 和 SOCKS5,如果你已经安装了 派索克斯或者你做了一个 Pip install urllib3[袜子]
唉,我用 Socks 尝试了 PySocks 和 urllib3,但都没有成功。
还有别的办法吗?
编辑:
我还尝试了 安装要求[袜子](这是使用 Socks 支持的请求2.10.0) ,我得到了这个结果:
File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 467, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='api-server.com', port=443): Max retries exceeded with url: /auth (Caused by NewConnectionError('<requests.packages.urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x95c7ccc>: Failed to establish a new connection: SOCKS5 proxy server sent invalid data',))