In [4]:
import stem
import stem.connection

import time
import urllib2

from stem import Signal
from stem.control import Controller

In [6]:
'''
Python script to connect to Tor via Stem and Privoxy, requesting a new connection (hence a new IP as well) as desired.
'''



# initialize some HTTP headers
# for later usage in URL requests
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent}

# initialize some
# holding variables
oldIP = "0.0.0.0"
newIP = "0.0.0.0"

# how many IP addresses
# through which to iterate?
nbrOfIpAddresses = 3

# seconds between
# IP address checks
secondsBetweenChecks = 2

# request a URL 
def request(url):
    # communicate with TOR via a local proxy (privoxy)
    def _set_urlproxy():
        proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
        opener = urllib2.build_opener(proxy_support)
        urllib2.install_opener(opener)

    # request a URL
    # via the proxy
    _set_urlproxy()
    request=urllib2.Request(url, None, headers)
    return urllib2.urlopen(request).read()

# signal TOR for a new connection 
def renew_connection():
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate(password = 'karaman')
        controller.signal(Signal.NEWNYM)
        controller.close()

# cycle through
# the specified number
# of IP addresses via TOR 
for i in range(0, nbrOfIpAddresses):

    # if it's the first pass
    if newIP == "0.0.0.0":
        # renew the TOR connection
        renew_connection()
        # obtain the "new" IP address
        newIP = request("http://icanhazip.com/")
    # otherwise
    else:
        # remember the
        # "new" IP address
        # as the "old" IP address
        oldIP = newIP
        # refresh the TOR connection
        renew_connection()
        # obtain the "new" IP address
        newIP = request("http://icanhazip.com/")

    # zero the 
    # elapsed seconds    
    seconds = 0

    # loop until the "new" IP address
    # is different than the "old" IP address,
    # as it may take the TOR network some
    # time to effect a different IP address
    while oldIP == newIP:
        # sleep this thread
        # for the specified duration
        time.sleep(secondsBetweenChecks)
        # track the elapsed seconds
        seconds += secondsBetweenChecks
        # obtain the current IP address
        newIP = request("http://icanhazip.com/")
        # signal that the program is still awaiting a different IP address
        print ("%d seconds elapsed awaiting a different IP address." % seconds)
    # output the
    # new IP address
    print ("")
    print ("newIP: %s" % newIP)


---------------------------------------------------------------------------
SocketError                               Traceback (most recent call last)
<ipython-input-6-9f74595bc3a3> in <module>()
     52     if newIP == "0.0.0.0":
     53         # renew the TOR connection
---> 54         renew_connection()
     55         # obtain the "new" IP address
     56         newIP = request("http://icanhazip.com/")

<ipython-input-6-9f74595bc3a3> in renew_connection()
     39 # signal TOR for a new connection
     40 def renew_connection():
---> 41     with Controller.from_port(port = 9051) as controller:
     42         controller.authenticate(password = 'karaman')
     43         controller.signal(Signal.NEWNYM)

/home/malc/anaconda2/lib/python2.7/site-packages/stem/control.pyc in from_port(address, port)
    996       control_port = stem.connection._connection_for_default_port(address)
    997     else:
--> 998       control_port = stem.socket.ControlPort(address, port)
    999 
   1000     return Controller(control_port)

/home/malc/anaconda2/lib/python2.7/site-packages/stem/socket.pyc in __init__(self, address, port, connect)
    370 
    371     if connect:
--> 372       self.connect()
    373 
    374   def get_address(self):

/home/malc/anaconda2/lib/python2.7/site-packages/stem/socket.pyc in connect(self)
    241 
    242       with self._recv_lock:
--> 243         self._socket = self._make_socket()
    244         self._socket_file = self._socket.makefile(mode = 'rwb')
    245         self._is_alive = True

/home/malc/anaconda2/lib/python2.7/site-packages/stem/socket.pyc in _make_socket(self)
    399       return control_socket
    400     except socket.error as exc:
--> 401       raise stem.SocketError(exc)
    402 
    403 

SocketError: [Errno 111] Connection refused

In [ ]:


In [ ]: