0

I try to change tor ip in python3 but it doesn't always work the same can you recommend me an effective method with python3?

import time
import socket
import socks
import requests

from urllib.request import urlopen
from stem import Signal
from stem.control import Controller


controller = Controller.from_port(port=9051)

def connectTor():
    socket.socket = None
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)
    socket.socket = socks.socksocket

def renewTor():
    controller.authenticate("xxxxxxx")
    controller.signal(Signal.NEWNYM)
     

def showIP():
    ip =requests.get('https://httpbin.org/ip',
        proxies = {
            'http': 'socks5://127.0.0.1:9050',
            'https': 'socks5://127.0.0.1:9050'
        }).text
    print(ip)

for i in range(5):
    renewTor()
    connectTor()
    showIP()
    time.sleep(30)


response of change IP

{ "origin": "185.220.131.43" }

{ "origin": "185.220.131.43" }

{ "origin": "185.220.131.43" }

{ "origin": "185.220.131.43" }

{ "origin": "185.220.131.43" }

SaxC
  • 1
  • 1
  • Does this answer your question? [How i can get new ip from tor every requests in threads?](https://stackoverflow.com/questions/56733397/how-i-can-get-new-ip-from-tor-every-requests-in-threads) – drew010 Oct 24 '22 at 23:12

0 Answers0