2

I'm trying to change my Tor IP address by using stem Python library. The code below works fine (I also use it for scraping), but the get_newnym_wait() always return "0" such that I can't define relevant spleep time before requesting a new IP.

See the MWE code below (just need to set up the controller port to your own to test it).

Thank you in advance for your help.

from stem import CircStatus
from stem import Signal
from stem.control import Controller
from random import randint
import time
from time import sleep

def renew_connection():
    with Controller.from_port(port = 9151) as controller:
        controller.authenticate();
        print controller.get_newnym_wait()
        time.sleep(controller.get_newnym_wait());
        controller.signal(Signal.NEWNYM);
    return;

def get_tor_exit_ip():
    with Controller.from_port(port = 9151) as controller:
        controller.authenticate()
        for circ in controller.get_circuits():
            if circ.status != CircStatus.BUILT:
                continue;
            exit_fp, exit_nickname = circ.path[-1]
            exit_desc = controller.get_network_status(exit_fp, None)
            exit_address = exit_desc.address if exit_desc else 'unknown'
        return exit_address;

a = 0
while a < 5:
    a = a + 1;
    renew_connection();
    print get_tor_exit_ip(); 
Bob
  • 43
  • 1
  • 1
  • 3
  • It's only ever going to return a number between 0 and 10. If you haven't issued the signal in the last 10 seconds, then it will be 0 and no wait required. – drew010 Jun 27 '17 at 21:00
  • @drew010 Thanks, but the example I provides does actually issue the signal in the last 10 seconds and `get_newnym_wait()` still returns 0. – Bob Jun 29 '17 at 10:02

0 Answers0