How to make python Requests work via socks proxy

How to make python Requests work via socks proxy

The modern way:

pip install -U requests[socks]


import requests

resp = requests.get(, 
                    proxies=dict(http=socks5://user:[email protected]:port,
                                 https=socks5://user:[email protected]:port))

As of requests version 2.10.0, released on 2016-04-29, requests supports SOCKS.

It requires PySocks, which can be installed with pip install pysocks.

Example usage:

import requests
proxies = {http: socks5://myproxy:9191}
requests.get(, proxies=proxies)

How to make python Requests work via socks proxy

In case someone has tried all of these older answers, and is still running into problems like:

   SOCKSHTTPConnectionPool(host=myhost, port=80): 
   Max retries exceeded with url: /my/path 
   (Caused by NewConnectionError(<requests.packages.urllib3.contrib.socks.SOCKSConnection object at 0x106812bd0>: 
   Failed to establish a new connection: 
   [Errno 8] nodename nor servname provided, or not known,))

It may be because, by default, requests is configured to resolve DNS queries on the local side of the connection.

Try changing your proxy URL from socks5://proxyhost:1234 to socks5h://proxyhost:1234. Note the extra h (it stands for hostname resolution).

The PySocks package module default is to do remote resolution, and Im not sure why requests made their integration this obscurely divergent, but here we are.

Leave a Reply

Your email address will not be published. Required fields are marked *