How to change tor id in python?

I have the following script:

import socks import socket socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) socket.socket = socks.socksocket import urllib2 print(urllib2.urlopen("http://www.ifconfig.me/ip").read()) 

which uses tor and SocksiPy

Now I want to change the identity with each request, for example:

 for i in range(0, 10): #somehow change tor identity print(urllib2.urlopen("http://www.ifconfig.me/ip").read()) 

How can i do this?

+20
python tor
Mar 27 '12 at 10:13
source share
6 answers

Today I searched a lot about this question and, finally, I was able to answer it myself. But before I have to say that piercings and torus must be set up correctly. First a script, then a little about configuration:

 import urllib2 from TorCtl import TorCtl proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"}) opener = urllib2.build_opener(proxy_support) def newId(): conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="your_password") conn.send_signal("NEWNYM") for i in range(0, 10): print "case "+str(i+1) newId() proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"}) urllib2.install_opener(opener) print(urllib2.urlopen("http://www.ifconfig.me/ip").read()) 

The above script gets the new IP address and checks it from the ifconfig.me website. About the configuration: We need Privoxy . To use TOR with HTTP connections, privoxy must work with tor. We can do this by adding thi to the / etc / privoxy / config file:

 forward-socks5 / localhost:9050 . #dot is important at the end 

then we configure ControlPort in the file / etc / tor / torrc. We just need to uncomment this line:

 ControlPort 9051 ## If you enable the controlport, be sure to enable one of these ## authentication methods, to prevent attackers from accessing it. HashedControlPassword 16:872860B76453A77D60CA2BB8C1A7042072093276A3D701AD684053EC4C 

then we just restart tor:

 /etc/init.d/tor restart 
+19
Mar 29 2018-12-12T00:
source share

Tor has written a new TOR management library in Python, stem . It can be found on PyPI . They provide some useful guides for working with him, one of them explains how to change your personality:

 from stem import Signal from stem.control import Controller with Controller.from_port(port = 9051) as controller: controller.authenticate() controller.signal(Signal.NEWNYM) 

Make sure your config is correct .

+26
Nov 06 '13 at
source share

Another simple solution that does not require external libraries works for both IPv4 and IPv6:

 import socket try: tor_c = socket.create_connection((TOR_CTRL_HOST, TOR_CTRL_PORT)) tor_c.send('AUTHENTICATE "{}"\r\nSIGNAL NEWNYM\r\n'.format(TOR_CTRL_PWD)) response = tor_c.recv(1024) if response != '250 OK\r\n250 OK\r\n': sys.stderr.write('Unexpected response from Tor control port: {}\n'.format(response)) except Exception, e: sys.stderr.write('Error connecting to Tor control port: {}\n'.format(repr(e))) 
+11
Jun 14 '13 at 14:41
source share

This is a video where im uses STEM, SockSipy, Tor 100% works :)

 #!/usr/bin/python import socks import socket import time from stem.control import Controller from stem import Signal import urllib2 import sys def info(): print "[*] Welcome to Chart-Cheat Script" print "[*] This script works with running TOR only" print "[*] usage is chartcheat.py domain" print "[*] argument domain must be in format www.example.com" print "[*] Example: chartcheat.py www.example.com" return if len(sys.argv)==2: info(); counter = 0 url = str(sys.argv[1]); with Controller.from_port(port = 9051) as controller: controller.authenticate() socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) socket.socket = socks.socksocket #visiting url in infinite loop while True: urllib2.urlopen("http://"+url) counter=counter+1 print "Page " + url + " visited = " + str(counter) #wait till next identity will be available controller.signal(Signal.NEWNYM) time.sleep(controller.get_newnym_wait()) else: info(); 
+1
Mar 08 '15 at 9:36
source share

You can enable the tor management server by uncommenting a few lines in

 /etc/tor/torrc 

And use the stem library to send the NEWNYM signal to change the pattern.

 controller.signal(Signal.NEWNYM) 

Here you can read the tutorial.

0
Feb 16 '16 at 8:21
source share

The following may work:

 for i in range(0, 10): #somehow change tor identity socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050+i) socket.socket = socks.socksocket print(urllib2.urlopen("http://www.ifconfig.me/ip").read()) 

Basically, you set up a proxy server before each connection. I assume that you have different permissions for different IP addresses, since you did not specify how you are going to change the IP address

-one
Mar 29 2018-12-12T00:
source share



All Articles