Skip to content

Instantly share code, notes, and snippets.

@zachwill
Created September 29, 2011 23:10
Show Gist options
  • Select an option

  • Save zachwill/1252190 to your computer and use it in GitHub Desktop.

Select an option

Save zachwill/1252190 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Use Tor through Python. Note, Tor must be running for the script to
work properly.
"""
import urllib2
def use_tor(url):
"""Use Tor to open some URL."""
proxy = urllib2.ProxyHandler({'http': '127.0.0.1:8118'})
opener = urllib2.build_opener(proxy)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
return opener.open(url).read()
if __name__ == '__main__':
url = "http://www.google.com"
print use_tor(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment