My goal is to use python's mechanize with a tor SOCKS proxy.
I am not using a GUI with the following Ubuntu version:
Description: Ubuntu 12.04.1 LTS
Release: 12.04
Codename: precise
Tor is installed and is listening on port 9050 according to the nmap scan:
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
I also thought it reasonable to see if I could telnet to port 9050, which I can:
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.
I had high hopes for the suggestion in this post to get tor working with urllib2:
How can I use a SOCKS 4/5 proxy with urllib2?
So I tried the following script in python:
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()
The script just hangs with no response.
I thought that since mechanize seems to be related to urllib2 that the following script might work:
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()
I get the same result as above with the urllib2 script.
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.
I ran this script and received an expected response. I did not use the tor proxy:
In [1]: import urllib2
In [2]: print urllib2.urlopen('http://icanhazip.com').read()
xxxx:xxxx:xxxx:512:13b2:ccd5:ff04:c5f4
Thanks.
I found something that works! I have no idea why it works, but it does. I found it here:
Python urllib over TOR?
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()
See Question&Answers more detail:
os