socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9050)
socket.socket = socks.socksocket

def newI():
    socket.socket = old_socket
with Controller.from_port(port=9051) as controller:
    controller.authenticate()
controller.signal(Signal.NEWNYM)
socket.socket = socks.socksocket
newI()
headers = {'User-Agent': 'Mozilla/3.0 (x86 [en] Windows NT 5.1; Sun)'}
req = urllib.request.Request('https://google.com', None, headers)
response = urllib.request.urlopen(req)
html = response.read()
newI()
Как управлять tor при использовании tor proxy