I’m writing a script in python.
My script uses urllib2 to read web pages.
I’m using Socksipy to force urllib2 to use socks proxy (TOR).
My problem is after setting socket.socket TorCtl doesn’t work and raises exception.
the code that doesn’t work is (first newTorId() works and second fails!):
newTorId() #This works
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
newTorId() #this Fails!
newTorId is defined as this:
from TorCtl import TorCtl
def newTorId():
conn = TorCtl.connect(passphrase="test")
conn.send_signal("NEWNYM")
The exception is:
__init__() takes exactly 2 arguments (3 given)
Traceback (most recent call last):
File "script.py", line 97, in <module>
newTorId()
File "script.py", line 27, in newTorId
conn.send_signal("NEWNYM")
AttributeError: 'NoneType' object has no attribute 'send_signal'
What is the problem?
It’s your own code raising the exception, not the library. Particularly,
is returning
Nonethe second time, resulting in an error when you call.send_signalon the result.I assume
newTorId()is intended to request a new Tor identity. You should probably just callTorCtl.Connection.send_signal(conn, "NEWNYM")to get a new identity, rather than trying to create a new connection each time. See Python – Controlling Tor.