new_contents: # -*- coding: utf-8 -*-
  
  import stem
  import stem.connection
  
  import time
  import urllib2
  
  from stem import Signal
  from stem.control import Controller
  
  # 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 = 'my_password')
          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)
  
subject: Create test_tor_stem_privoxy.py