Python script exception with Tor

I have the following script that uses SocksiPY

and Tor:

from TorCtl import TorCtl import socks import socket socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) socket.socket = socks.socksocket import urllib2 import sqlite3 from BeautifulSoup import BeautifulSoup def newId(): conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123") TorCtl.Connection.send_signal(conn, "NEWNYM") newId() print(urllib2.urlopen("http://www.ifconfig.me/ip").read()) 

This code should change the Tor identifier, but it waits a while and gives the following error:

 tuple index out of range Traceback (most recent call last): File "template.py", line 16, in <module> newId() File "template.py", line 14, in newId TorCtl.Connection.send_signal(conn, "NEWNYM") TypeError: unbound method send_signal() must be called with Connection instance as first argument (got NoneType instance instead) 

But the above script is divided into 2 separate scripts:

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

and

 from TorCtl import TorCtl def newId(): conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123") TorCtl.Connection.send_signal(conn, "NEWNYM") newId() 

When the second script is called, it is first called ok. Can someone explain what the problem is and how to fix it?

+5
python tor
Mar 29 2018-12-12T00:
source share
2 answers

The anonymous author explained very well that this socket is being overwritten, the answer is almost perfect, except that you have to close the control socket . This is safer because of the TorCtl event loop, but I have to look deeper into the TorCtl code to understand this event loop.

To summarize your code, will be:

 from TorCtl import TorCtl import socks import socket socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) import urllib2 import sqlite3 from BeautifulSoup import BeautifulSoup __originalSocket = socket.socket def newId(): ''' Clean circuit switcher Restores socket to original system value. Calls TOR control socket and closes it Replaces system socket with socksified socket ''' socket.socket = __originalSocket conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123") TorCtl.Connection.send_signal(conn, "NEWNYM") conn.close() socket.socket = socks.socksocket newId() print(urllib2.urlopen("http://www.ifconfig.me/ip").read()) 
+5
Apr 17 '12 at 15:14
source share

The connection to the management port fails, and conn assigned a value that uses Python sockets to indicate a failure (which, apparently, is of type NoneType ).

The reason is that in the statement socket.socket = socks.socksocket , apparently you are replacing the default socket object or class with one that transparently proxies everything through Tor, which causes the program to try to proxy your connection to the control port.

The solution should only execute socket.socket = socks.socksocket after you open the control connection (and maintain this connection if you need it later) or keep the original socket.socket value socket.socket that you can switch between the values ​​as needed.

+2
Apr 14 '12 at 16:13
source share



All Articles