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)
@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