Skip to content

Instantly share code, notes, and snippets.

@tarcisio-marinho
Created January 28, 2019 16:13
Show Gist options
  • Save tarcisio-marinho/4e332b3f44f8faf87dc2efd824a46690 to your computer and use it in GitHub Desktop.
Save tarcisio-marinho/4e332b3f44f8faf87dc2efd824a46690 to your computer and use it in GitHub Desktop.
change IP address using tor with Python3
# change IP via tor with python
import requests
from stem import Signal
from stem.control import Controller
import socks, socket
import time
# tuts
# https://www.torproject.org/docs/faq.html.en#torrc
# https://stem.torproject.org/tutorials/the_little_relay_that_could.html
# https://stackoverflow.com/questions/9887505/how-to-change-tor-identity-in-python
# sudo pip3 install stem
# sudo apt-get install tor
# generate new password for tor > tor --hash-password "<new_password>"
# save new password hash -> /etc/tor on line -> HashedControlPassword
# uncomment HashedControlPassword and ControlPort
# restart tor service -> /etc/init.d/tor restart -> sudo service tor restart
def changeIp(current_ip):
controller.signal(Signal.NEWNYM)
time.sleep(controller.get_newnym_wait())
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='sexo')
socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, addr="127.0.0.1", port=9050)
socket.socket = socks.socksocket
current_ip = requests.get(url='http://icanhazip.com/')
print(current_ip.text)
changeIp(current_ip.text)
current_ip = requests.get(url='http://icanhazip.com/')
print(current_ip.text)
@AnonCoderymous
Copy link

Error :

Traceback (most recent call last):
File "/root/tor.py", line 25, in
controller.authenticate(password='Huzefa')
File "/usr/local/lib/python3.9/dist-packages/stem/control.py", line 1092, in authenticate
stem.connection.authenticate(self, *args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/stem/connection.py", line 592, in authenticate
controller._post_authentication()
File "/usr/local/lib/python3.9/dist-packages/stem/control.py", line 3772, in _post_authentication
owning_pid = self.get_conf('__OwningControllerProcess', None)
File "/usr/local/lib/python3.9/dist-packages/stem/control.py", line 2117, in get_conf
entries = self.get_conf_map(param, default, multiple)
File "/usr/local/lib/python3.9/dist-packages/stem/control.py", line 2224, in get_conf_map
for key in reply:
RuntimeError: dictionary keys changed during iteration

@Rayan25062011
Copy link

Error:

Connection refused[Errno 61]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment