Using python requests over Tor

Problem:

You want to use the python requests library over a Tor connection using the Tor builtin SOCKS proxy.

Solution

First ensure that Tor is actually running. We will use port 9050 (the default) as SOCKS port for this example.

Unfortunately, requests does not natively support SOCKS proxies yet (see this issue for details). Therefore you have to use requesocks, a quite old fork of requests. Note that you might not be able to use recently added requests methods

Install requesocks by running pip install requesocks.

Here’s a short example returning your Tor-anonymized IP:

#!/usr/bin/env python
#Released under CC0
import requesocks
#Initialize a new wrapped requests object
session = requesocks.session()
#Use Tor for both HTTP and HTTPS
session.proxies = {'http': 'socks5://localhost:9050', 'https': 'socks5://localhost:9050'}
#fetch a page that shows your IP address
response = session.get('http://httpbin.org/ip')
print(response.text)

Note that requesocks currently doesn’t work with Python3. As an alternative, you could use pycurl as described here or SocksiPyas urllib2 wrapper as described here.