Search code examples
pythontor

I am trying to work with stem but getting error


I am trying to change TOR ip by my script. It is my fucnvtion to make NEWNYM in tor:

from stem.connection import connect_port
from stem import Signal
from stem.control import Controller
def connectTor():
    controller = connect_port(address='127.0.0.1', port=9150)
    controller.connect_port()
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

Getting error:

TypeError: __init__() takes exactly 2 arguments (3 given)

What i am doing wrong? Can You help me? Thank You!

UPD: Full Traceback:

Traceback (most recent call last):
  File "D:/python/projects/tor/main.py", line 33, in <module>
    connectTor()
  File "D:/python/projects/tor/main.py", line 15, in connectTor
    controller = connect_port(port=9150)
  File "C:\Python27\32\lib\site-packages\stem\connection.py", line 171, in connect_port
    control_port = stem.socket.ControlPort(address, port)
  File "C:\Python27\32\lib\site-packages\stem\socket.py", line 350, in __init__
    self.connect()
  File "C:\Python27\32\lib\site-packages\stem\socket.py", line 223, in connect
    self._socket = self._make_socket()
  File "C:\Python27\32\lib\site-packages\stem\socket.py", line 376, in _make_socket
    control_socket.connect((self._control_addr, self._control_port))
  File "D:\python\projects\tor\socks.py", line 369, in connect
    self.__negotiatesocks5(destpair[0],destpair[1])
  File "D:\python\projects\tor\socks.py", line 236, in __negotiatesocks5
    raise Socks5Error(ord(resp[1]),_generalerrors[ord(resp[1])])
TypeError: __init__() takes exactly 2 arguments (3 given)

P.S. Sorry for my bad english...


Solution

  • The connect_port() method gives you a Controller instance. The Controller in turn doesn't have its own 'connect_port()' method so I'm surprised that "controller.connect_port()" line isn't balking.

    Repeatedly cycling your IP is discouraged as it harms the network. Please see...

    https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor