Crawling anonymously with Tor in Python

March 5, 2014

There are a lot of valid usecases when you need to protect your identity while communicating over the public internet. It is 2013 and so you probably already know about Tor. Most people use Tor through the browser. The cool thing is that you can get access to the Tor network programmatically so you can build interesting tools with privacy built into it.

The most common usecase to be able to hide the identity using TOR or being able to change identities programmatically is when you are crawling a website like Google (well, this one is harder than you think) and you don’t want to be rate-limited or forbidden.

This did take a fair amount hit and trial to get it working though.
First of all, lets install Tor.

apt-get update
apt-get install tor
/etc/init.d/tor restart

You will notice that socks listener is on port 9050.

Lets enable the ControlPort listener for Tor to listen on port 9051. This is the port Tor will listen to for any communication from applications talking to Tor controller. The Hashed password is to enable authentication to the port to prevent any random access to the port.

You can create a hashed password out of your password using:

tor --hash-password mypassword

So, update the torrc with the port and the hashed password.


ControlPort 9051
HashedControlPassword 16:872860B76453A77D60CA2BB8C1A7042072093276A3D701AD684053EC4C

Restart Tor again to the configuration changes are applied.

/etc/init.d/tor restart

Next, we will install pytorctl which is a python based module to interact with the Tor Controller. This lets us send and receive commands from the Tor Control port programmatically.

apt-get install git
apt-get install python-dev python-pip
git clone git://
pip install pytorctl/

Tor itself is not a http proxy. So in order to get access to the Tor Network, we will use the Privoxy as an http-proxy though socks5..

Install Privoxy.

apt-get install privoxy

Now lets tell privoxy to use TOR. This will tell Privoxy to route all traffic through the SOCKS servers at localhost port 9050.
Go to /etc/privoxy/config and enable forward-socks5:

forward-socks5 / localhost:9050 .

Restart Privoxy after making the change to the configuration file.

/etc/init.d/privoxy restart

In the script below, we’re using urllib2 to use the proxy. Privoxy listens on port 8118 by default, and forwards the traffic to port 9050 which the Tor socks is listening on.
Additionally, in the renew_connection() function, I am also sending signal to Tor controller to change the identity, so you get new identities without restarting Tor. You don’t have to change the ip, but sometimes it comes in handy with you are crawling and don’t wanted to be blocked based on ip.

from TorCtl import TorCtl
import urllib2

user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009021910 Firefox/3.0.7'

def request(url):
    def _set_urlproxy():
        proxy_support = urllib2.ProxyHandler({"http" : ""})
        opener = urllib2.build_opener(proxy_support)
    request=urllib2.Request(url, None, headers)
    return urllib2.urlopen(request).read()

def renew_connection():
    conn = TorCtl.connect(controlAddr="", controlPort=9051, passphrase="your_password")

for i in range(0, 10):
    print request("")

Running the script:


Now, watch your ip change every few seconds.

Use it, but don’t abuse it.

31 Comments on Crawling anonymously with Tor in Python

Respond | Trackback

  1. Arul says:


    Thanks for this tutorial. How you are getting the hashed password..?

    I try to use this

    tor –hash-password your_password

    But every time it returns different hash password. How to do that.?


  2. Pavan says:


    Thanks for this tutorial. I have tried everything as you mentioned in the above tutorial and my program also running but 10 times i am getting the same ip address only.

    Please, solve my problem that will be helpful.


    • coolcoolcoolia says:

      I ran into this issue also and found a fix. It takes time to change identity through tor, so I edited the file to handle the issue.

      1) Create two new variables underneath the “headers” variable:

      oldIP = “″
      newIP = “″

      2) Change the for loop to:

      for i in range(0, 10):
          if oldIP == "":
              oldIP = request("")
      	oldIP = request("")
      	newIP = request("")
          while oldIP == newIP:
          	newIP = request("")
          print request("")
  3. Ballman50 says:


    J’ve an “invalid syntax” for print request

    Could you help me ?


  4. Tiago says:

    On line 17 what shoul I fill out on the password (passphrase=”your_password”)?

  5. sbleecpa says:

    헐퀴…별게 다 있구만 ㄷㄷㄷㄷㄷ

  6. […] is a socks proxy. Connecting to it directly with the example you cite fails with “urlopen error Tunnel connection failed: 501 Tor is not an HTTP Proxy”. As […]

  7. george militaru says:

    Hi all,
    I am new to this kind of things.I want to start an automhated ,,traing” system with some associates but my local provider is blocking me.If there is somewone interested to research this ideea and join the project and think please drop me an email at :

  8. Usuario PTC says:

    Hello, greetings to all … Initially thank for their work and for sharing.

    Home in the field of programming and running the code gives me the following error:

    Failed to read authentication cookie (permission denied): /var/run/tor/control.authcookie
    Traceback (most recent call last):
    File “”, line 22, in
    File “”, line 18, in renew_connection
    AttributeError: ‘NoneType’ object has no attribute ‘send_signal’

    I wonder … if additional configuration is required TOR I’m running bad or something.

    Thank you for your understanding and support on this issue.

    • Italian_Friend says:

      Your TOR may be version, you need to udpate to
      here the currect procedure

    • here2help says:

      You need to remove the hashtags from the relevant lines in the torrc file.

      Specifically, the lines that read

      ## The port on which Tor will listen for local connections from Tor
      ## controller applications, as documented in control-spec.txt.
      #ControlPort 9051
      ## If you enable the controlport, be sure to enable one of these
      ## authentication methods, to prevent attackers from accessing it.
      #HashedControlPassword ……

      should read

      ## The port on which Tor will listen for local connections from Tor
      ## controller applications, as documented in control-spec.txt.
      ControlPort 9051
      ## If you enable the controlport, be sure to enable one of these
      ## authentication methods, to prevent attackers from accessing it.
      HashedControlPassword …….

    • I think you had already solved your problem, but it may help other with the same issue.
      Make sure you have tor or above and try launch the script using sudo:

      sudo python

      I had the got the same error, and it solved my problem.

  9. f4lse says:

    Thanks for the the simple and effective tutorial.
    Works like a breeze with 0 errors on:

    Ubuntu Server 15.10
    python 2.7.10

  10. Abi says:


    I m getting an error
    Connection refused. Is the ControlPort enabled?
    Traceback (most recent call last):
    File “”, line 22, in
    File “”, line 18, in renew_connection
    AttributeError: ‘NoneType’ object has no attribute ‘send_signal’

    I try different port but no use

  11. B98 says:

    Hey guys,

    I was wandering if it is possible to trace you back if using this kind of crawling.

    Someone have enough experience to answer this question?

    Any help will be warmly received

  12. here2help says:

    hi! this is awesome, but why did you use TorCtl instead of Stem? TorCtl’s been deprecated for some time, and occasionally spits errors. I think Stem may be a cleaner option, in this case.

    thanks again for the tutorial!!

  13. PegasusWang says:

    Now requests support socks proxy. version >= 2.10.0

  14. markus says:

    Also with the same issue.

    After update my tor version I have the same error

    “Connection refused. Is the ControlPort enabled?”

  15. Alexandre cavalcante says:

    more than 3 years and it still working great!!!

  16. kn16h7 says:


    How do i crawl a router and scrap all the content after login?

  17. rez says:

    –hash-password is giving me different hashes for the same password, does anyone know why this is?

  18. pavan sharma says:

    When i am running .Py file then getting same Ip again and again

  19. pavan sharma says:

    Hi all,

    Please help me .
    When I run the above script then getting same IP.

  20. Tung Pham says:

    you should not call _set_urlproxy() every openurl request

  21. levent says:

    please help me. i have gmail brute force code… I don’t know how to run this program with tor.I need to change the ip after 5 passwords…
    can you help me? my gmail brute force code…

    import smtplib

    smtpserver = smtplib.SMTP(“”, 587)

    user = raw_input(“Hedef mail adresini giriniz: “)
    passwfile = raw_input(“sifreler listesinin adini yaziniz: “)
    passwfile = open(passwfile, “r”)

    for password in passwfile:
    smtpserver.login(user, password)

    print “[+] sifre bulundu: %s” % password
    except smtplib.SMTPAuthenticationError:
    print “[!] sifre bulunamadi: %s” % password

    please help me.can someone write me this with tor ? my gmail adress please help me…

  22. bilgibank says:

    well narrated but I get error in some places

Respond to Arul