简体   繁体   中英

Getting new identity with tor and selenium. got error “IncorrectSocketType: unable to use the control socket”

I'm trying to send a 'new identity' signal to tor using the stem module for python. I got most of the code that got me to this point from reading other questions in stackoverflow but I can't seem to find a way to fix this error. I want to be able to control tor with selenium and get a new identity any way possible. I'm just using stem since it seems like the way to go from the research I've made but I'm open to change.(I'm on windows)

I have tried changing

profile.set_preference('network.proxy.socks_port', 9050)

from 9050 to 9051 but I get

stem.SocketError: [WinError 10061] No connection could be made because the target machine actively refused it

I have looked at this post but I don't have any password set in my torrc file Unable to use Stem and Tor in Python to change my IP address?

I have also tried changing to CookieAuthentication 0 but changed it back to 1 since no luck with that either.

I open tor with selenium using this method:

def setup():
    torexe = os.popen(r'D:\Program files\tor\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    profile = FirefoxProfile(r'D:\Program files\tor\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.socks', '127.0.0.1')
    profile.set_preference('network.proxy.socks_port', 9050)
    profile.set_preference("network.proxy.socks_remote_dns", False)
    profile.update_preferences()
    driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\Windows\geckodriver.exe')
    driver.get("http://check.torproject.org")

and tried making this method to call a new identity for tor:

def new_identity():
    with Controller.from_port(port = 9050) as controller:
        controller.authenticate()
        controller.signal(Signal.NEWNYM)

my torrc file is also missing a lot of things from what I've seen on the internet but I have also read that I should be fine for it to look like this:

# This file was generated by Tor; if you edit it, comments will not be preserved
# The old torrc file was renamed to torrc.orig.1 or similar, and Tor will ignore it
ControlPort 9050
CookieAuthentication 0

DataDirectory D:\Program files\tor\Tor Browser\Browser\TorBrowser\Data\Tor
GeoIPFile D:\Program files\tor\Tor Browser\Browser\TorBrowser\Data\Tor\geoip
GeoIPv6File D:\Program files\tor\Tor Browser\Browser\TorBrowser\Data\Tor\geoip6

the error im curently getting is:

raise IncorrectSocketType('unable to use the control socket')
stem.connection.IncorrectSocketType: unable to use the control socket

The default SocksPort is 9050, and the ControlPort (which is not enabled by default) is 9051.

You'll need both of those configuration options in order to get what you want working.

The browser SOCKS config should use port 9050, and your Python code for stem should use 9051. To start, leave CookieAuthentication and PasswordAuthentication options out, so you should be able to connect locally without auth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM