Tor browser, new IP not working?

痞子三分冷 提交于 2019-12-20 06:12:49

问题


I am trying to use tor browser, and get a new IP address each URL I visit in python. I am able to open an instance of selenium running the tor browser, but how can I request a new IP every website I visit?

binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
if os.path.exists(binary) is False:
    raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)


browser = None
def get_browser(binary=None):
    browser = webdriver.Firefox(firefox_binary=binary)
    return browser
if __name__ == "__main__":
    browser = get_browser(binary=firefox_binary)
    urls = (
        ('tor browser check', 'https://check.torproject.org/'),
        ('ip checker', 'http://icanhazip.com')
    )
    for url_name, url in urls:
        print "getting", url_name, "at", url
        browser.get(url)

回答1:


To use Python to request a new IP for every request, you need to open a connection to the ControlPort and issue a NEWNYM signal.

You can use Stem to simplify the connection and commands:

from stem.control import Controller
from stem import Signal

if __name__ == '__main__':
  with Controller.from_port(port = 9051) as controller:
    controller.authenticate('password')  # provide the password here if you set one

    controller.signal(Signal.NEWNYM) # switch to clean circuits

Keep in mind Tor may rate limit NEWNYM requests so you may need to wait a short while (default 10 seconds) before issuing that command. Also, due to the limited number of exit nodes, your circuits might get the same exit node depending on how many requests you are issuing.

You need to issue this command every time you want to get a new IP (switch circuits).



来源:https://stackoverflow.com/questions/33490484/tor-browser-new-ip-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!