title: using tor as a SOCKS5 proxy with python urllib2 or mechanize
body: <p>My goal is to use python's mechanize with a tor SOCKS proxy.</p>
  
  <p>I am not using a GUI with the following Ubuntu version:
          Description:    Ubuntu 12.04.1 LTS
          Release:    12.04
          Codename:   precise</p>
  
  <p>Tor is installed and is listening on port 9050 according to the nmap scan:</p>
  
  <pre><code>    Starting Nmap 5.21 ( http://nmap.org ) at 2013-01-22 00:50 UTC
      Nmap scan report for localhost (127.0.0.1)
      Host is up (0.000011s latency).
      Not shown: 996 closed ports
      PORT     STATE SERVICE
      22/tcp   open  ssh
      80/tcp   open  http
      3306/tcp open  mysql
      9050/tcp open  tor-socks
  </code></pre>
  
  <p>I also thought it reasonable to see if I could telnet to port 9050, which I can:</p>
  
  <pre><code>    telnet 127.0.0.1 9050
      Trying 127.0.0.1...
      Connected to 127.0.0.1.
      Escape character is '^]'.
      quit
      Connection closed by foreign host.
  </code></pre>
  
  <p>I had high hopes for the suggestion in this post to get tor working with urllib2:
  <a href="https://stackoverflow.com/questions/2317849/how-can-i-use-a-socks-4-5-proxy-with-urllib2">How can I use a SOCKS 4/5 proxy with urllib2?</a></p>
  
  <p>So I tried the following script in python:</p>
  
  <pre><code>    import socks
      import socket
      socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
      socket.socket = socks.socksocket
      import urllib2
      print urllib2.urlopen('http://icanhazip.com').read()
  </code></pre>
  
  <p>The script just hangs with no response.</p>
  
  <p>I thought that since mechanize seems to be related to urllib2 that the following script might work:</p>
  
  <pre><code>    import socks
      import socket
      import mechanize
      from mechanize import Browser
      socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
      socket.socket = socks.socksocket
      br = Browser()
      print br.open('http://icanhazip.com').read()
  </code></pre>
  
  <p>I get the same result as above with the urllib2 script.</p>
  
  <p>I am very new to python and networking, so I need a second opinion on how to make the python urllib2 use tor as a SOCKS on a non-GUI Ubuntu server.</p>
  
  <p>I ran this script and received an expected response. I did not use the tor proxy:</p>
  
  <pre><code>    In [1]: import urllib2
  
      In [2]: print urllib2.urlopen('http://icanhazip.com').read()
      xxxx:xxxx:xxxx:512:13b2:ccd5:ff04:c5f4
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>I found something that works! I have no idea why it works, but it does. I found it here: 
  <a href="https://stackoverflow.com/questions/5148589/python-urllib-over-tor?rq=1">Python urllib over TOR?</a></p>
  
  <pre><code>    import socks
      import socket
      def create_connection(address, timeout=None, source_address=None):
          sock = socks.socksocket()
          sock.connect(address)
          return sock
  
      socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
  
      # patch the socket module
      socket.socket = socks.socksocket
      socket.create_connection = create_connection
  
      import urllib2
  
      print urllib2.urlopen('http://icanhazip.com').read()
  
      import mechanize
      from mechanize import Browser
  
      br = Browser()
      print br.open('http://icanhazip.com').read()
  </code></pre>