using tor as a SOCKS5 proxy with python urllib2 or

2020-02-09 18:16发布

问题:

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()

回答1:

See end of question.

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()


回答2:

The above solution didn't work for me. I am on Ubuntu 14.04. Whenever I try to run the above script it keeps throwing the following error.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
  return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 404, in open
  response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 422, in _open
  '_open', req)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
  result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
  return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
  raise URLError(err)
urllib2.URLError: <urlopen error ((1, 'general SOCKS server failure'),)>

Checked if tor is running by using the nmap command.

Nmap scan report for localhost (127.0.0.1)
Host is up (0.00026s latency).
Not shown: 993 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
631/tcp  open  ipp
902/tcp  open  iss-realsecure
3306/tcp open  mysql
9050/tcp open  tor-socks

Installing Vidalia solved this problem. Apparently, the socks proxy was not allowing the connection to pass through it. Hope this might help someone facing the same problem.