I'm trying to interact with an API from my Python 2.7 shell using a package that relies on Python's requests. Thing is the remote address is blocked by my network (university library).
So to speak to the API I do the following:
~$ ssh -D 8080 name@myserver.com
And then, in new terminal, in local computer:
~$ export http_proxy=socks5://127.0.0.1:8080 https_proxy=socks5://127.0.0.1:8080
Then I run the program in Python console but fails:
~$ 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.
This excerpt is from the adapters.py requests module:
> try:
> from .packages.urllib3.contrib.socks import SOCKSProxyManager except ImportError:
> def SOCKSProxyManager(*args, **kwargs):
> raise InvalidSchema("Missing dependencies for SOCKS support.")
Now problem seems to be originated in urllib3's SOCKSProxyManager.
So I read you can use SOCKSProxyManager with SOCKS5 if you have install PySocks or you do a pip install urllib3[socks]
Alas, I tried both PySocks and urllib3 with Socks without any success.
Any idea of another workaround?
EDIT:
I also tried pip install requests[socks] (that's requests 2.10.0 with Socks support) and I am getting this:
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',))
I had the same issue with conda and requests 2.11 (I work in a Ubuntu VM behind a corporate proxy).
This issue helped me. I changed my environment variable all_proxy
(which was originally set to a SOCK proxy socks://....
) to the https version in my .bashrc file :
export all_proxy="https://<proxy>:<port>/"
and now it works.