I'm trying to connect to a relay on another machine using stem. My relay has the following torrc:

# torrc

SocksPort 0
ControlPort 9051
HashedPassword 16:F300D7EE5C7AEE6A601814EA742ACD3AF8C5BB3D888FCFB0C2A0672133
CookieAuthentication 1

Based on the example at https://stem.torproject.org/api/control.html, I wrote the script below, which stops at 'Unable to connect to port 9051'.

import sys

import stem.connection
import stem.socket

if __name__ == '__main__':
        ctrl_sckt = stem.socket.ControlPort(address='', port=9051)
    except stem.SocketError as e:
        print('Unable to connect to port 9051')

        cntrllr = stem.connection.authenticate_password(ctrl_sckt, 'psswrd')
    except stem.connection.PasswordAuthFailed as e:
        print('Could not authenticate')
        bytes_read = cntrllr.get_info('traffic/read')
        bytes_written = cntrllr.get_info('traffic/written')
        print('My relay read {} bytes and written {}.'.format(bytes_read,

I can verify my relay on the other machine is running by sshing into it and running nyx. Are there any additional configurations I must set in my torrc or is there a problem with my script that's preventing me from connecting to my relay?

  • The control port only binds on localhost, so it can’t be accessed from other computers. You can change this, but it’s highly discouraged to do this with a live relay. You’re better off using an ssh tunnel. See this answer (and the comments) for details: stackoverflow.com/a/45902073 – Steve Apr 6 '19 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.