How to make urllib2 requests through Tor in Python?

You are trying to connect to a SOCKS port – Tor rejects any non-SOCKS traffic. You can connect through a middleman – Privoxy – using Port 8118.

Example:

proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support) 
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
print opener.open('http://www.google.com').read()

Also please note properties passed to ProxyHandler, no http prefixing the ip:port

Leave a Comment