I am unable to use the function get_ports() with stem. It says

# python get_ports.py OR
[...]
  File "/usr/local/lib/python2.7/dist-packages/stem/socket.py", line 126, in send
raise stem.SocketClosed()

get_ports.py contains the following:

import stem
from stem.control import Controller
import sys

with Controller.from_port(port = 9051) as controller:
controller.authenticate()  # provide the password here if you set one
print controller.get_ports('OR')

I am certain my control port is open. My torrc has the following non-commented lines:

CookieAuthentication 1
ControlPort 9051
StrictNodes 1

Why can't I connect to my control port and how to execute the get_ports() function properly?


PS (full output)

# python get_ports.py                                     [git][stem/controller][master]
Traceback (most recent call last):
  File "get_ports.py", line 11, in <module>
    print controller.get_ports('OR')
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 454, in wrapped
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 1302, in get_ports
    return [port for (addr, port) in self.get_listeners(listener_type) if addr == '127.0.0.1']
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 454, in wrapped
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 1334, in get_listeners
    for listener in self.get_info(query).split():
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 454, in wrapped
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 1162, in get_info
    response = self.msg('GETINFO %s' % ' '.join(params))
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 627, in msg
    self._socket.send(message)
  File "/usr/local/lib/python2.7/dist-packages/stem/socket.py", line 126, in send
    raise stem.SocketClosed()
stem.SocketClosed

Works for me:

$ pip2 show stem
---
Name: stem
Version: 1.5.1
Location: /usr/local/lib/python2.7/dist-packages
Requires: 
$ python
Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from stem.control import Controller
>>> with Controller.from_port(port = 9151) as controller:
...  controller.authenticate()
...  print controller.get_ports('OR')
... 
[]
>>> with Controller.from_port(port = 9151) as controller:
...  controller.authenticate()
...  print controller.get_ports('CONTROL')
... 
[9151]

Are you running this under Tails, Whonix, Subgraph or a similar operating system which uses a control port filter as a security mechanism?

The connection is being terminated from somewhere outside after the GETINFO request is sent, as shown by the stem.SocketClosed exception being raised, which took all of 1 minute to find. You should familiarize yourself with utilizing the documentation of libraries if you intend to write code, it is an invaluable skill.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.