My goal is to use python mechanics with SOCKS proxies.
I do not use the GUI with the next version of Ubuntu: Description: Ubuntu 12.04.1 LTS Issue: 12.04 Codename: accurate
Tor is installed and listens on port 9050 according to 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 found it reasonable to see if I can use telnet for 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 a suggestion in this post to make working with urllib2: How to use 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 without an answer.
I thought that since mechanization seems to be related to urllib2, 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 urllib2 script.
I am very new to python and the web, so I need a second opinion on how to get python urllib2 to use tor as SOCKS on a server without a Ubuntu GUI.
I ran this script and got the expected response. I did not use tor proxy:
In [1]: import urllib2 In [2]: print urllib2.urlopen('http://icanhazip.com').read() xxxx:xxxx:xxxx:512:13b2:ccd5:ff04:c5f4
Thank.
I found something that works! I have no idea why this 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)