Yuwen Yan
Yuwen Yan

Reputation: 4935

Urllib2 using tor in python

I'm trying to crawl websites using a crawler written in Python, and want to integrate Tor with Python meaning I want to crawl the site anonymously using Tor.

I have found some answers on stackoverflow, but none of they work for me.


Here is the first solution I found from Urllib2 using Tor and socks in python

import socks
import socket
import urllib2    
socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, "127.0.0.1", 9050)
socket.socket = socks.socksocket
print urllib2.urlopen('http://my-ip.herokuapp.com').read()

but I get below error

(501, 'Tor is not an HTTP Proxy')

then, the accepted answer from How can I use a SOCKS 4/5 proxy with urllib2?

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8080)
socket.socket = socks.socksocket
import urllib2
print urllib2.urlopen('http://www.google.com').read()

I get below error

<urlopen error [Errno 111] Connection refused>

then, the top voted answer from 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

My test url is "http://almien.co.uk/m/tools/net/ip/", above code will run for 2 minutes, and end with below error

  File "/usr/lib/python2.7/dist-packages/socks.py", line 369, in connect
    self.__negotiatesocks5(destpair[0],destpair[1])
  File "/usr/lib/python2.7/dist-packages/socks.py", line 236, in __negotiatesocks5
    raise Socks5Error(ord(resp[1]),_generalerrors[ord(resp[1])])
IndexError: tuple index out of range

someone commented that the latest port is 9150 but 9050, so I tried with 9150 again, and get below error

urllib2.URLError: <urlopen error [Errno 111] Connection refused>

UPDATE

Add tor information on my machine.

root@xxxxxxx:~# tor
Apr 22 14:14:39.818 [notice] Tor v0.2.4.20 (git-0d50b03673670de6) running on Linux with Libevent 2.0.21-stable and OpenSSL 1.0.1f.
Apr 22 14:14:39.818 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Apr 22 14:14:39.818 [notice] Read configuration file "/etc/tor/torrc".
Apr 22 14:14:39.820 [notice] Opening Socks listener on 127.0.0.1:9050
Apr 22 14:14:39.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Apr 22 14:14:39.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Apr 22 14:14:39.000 [warn] You are running Tor as root. You don't need to, and you probably shouldn't.
Apr 22 14:14:39.000 [warn] OpenSSL version from headers does not match the version we're running with. If you get weird crashes, that might be why. (Compiled with 1000105f: OpenSSL 1.0.1e 11 Feb 2013; running with 1000106f: OpenSSL 1.0.1f 6 Jan 2014).
Apr 22 14:14:40.000 [notice] Bootstrapped 5%: Connecting to directory server.

Upvotes: 0

Views: 1627

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

Start tor then:

import socket
import urllib

import socks  # SocksiPy module
import stem.process

SOCKS_PORT = 9050

# Set socks proxy and wrap the urllib module

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket

# Perform DNS resolution through the socket

def getaddrinfo(*args):
  return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

print urllib.urlopen('http://my-ip.herokuapp.com').read()

Based on the to_russia_with_love code using stem. If you want to also start tor from python you should check out stem.

Upvotes: 1

Related Questions