edA-qa mort-ora-y edA-qa mort-ora-y - 2 years ago 73
Python Question

requests via a SOCKs proxy

How can I make an HTTP request via a SOCKs proxy (simply using

ssh -D
as the proxy)? I've tried using requests with SOCK proxies but it doesn't appear to work (I saw this pull request). For example:

proxies = { "http": "socks5://localhost:9999/" }
r = requests.post( endpoint, data=request, proxies=proxies )

It'd be convenient to keep using the requests library, but I can also switch to urllib2 if that is known to work.

Answer Source

SOCKS support for requests is still pending. If you want, you can view my Github repository here to see my branch of the Socksipy library. This is the branch that is currently being integrated into requests; it will be some time before requests fully supports it, though.


It should work okay with urllib2. Import sockshandler in your file, and follow the example inside of it. You'll want to create an opener like this:

opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "localhost", 9050))

Then you can use opener.open(url) and it should tunnel through the proxy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download