How to route urllib requests through the TOR network?

Lobe picture Lobe · Apr 2, 2009 · Viewed 10k times · Source

How to route urllib requests through the TOR network?

Answer

jahmax picture jahmax · Jul 24, 2010

This works for me (using urllib2, haven't tried urllib):

def req(url):
    proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
    opener = urllib2.build_opener(proxy_support) 
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    return opener.open(url).read()

print req('http://google.com')