0

I have installed Tor on a command-line only environment without the browser, Tor Socks Proxy is running on 9050.

To check if the proxy code is running correctly i used:

% curl --socks5 localhost:9050 --socks5-hostname localhost:9050 -s https://check.torproject.org/ | cat | grep -m 1 Congratulations | xargs

Which shows:

Congratulations. This browser is configured to use Tor.

But i need to do this test programmatically with python.

How can i programmatically test if the tor socks5 proxy is running and that it configured correctly using a python script?

Thanks

0

I personally wouldn't do this by getting a web page. It might depend on your use case but if you want to get status info about Tor you probably should get this info from the Tor control port (9051). You will have to turn on the control port in your torrc file first.

Then you can either use STEM (which is in python) or you can get the status manually with some code like this (don't forget to put in your control port password that you specified in torrc):

import socket
import sys

try:
    tor_c = socket.create_connection(('127.0.0.1', 9051))
    tor_c.send('AUTHENTICATE "{}"\r\nGETINFO status/circuit-established\r\nQUIT\r\n'.format('YOUR_PWD'))
    response = tor_c.recv(1024)    
    print response
    if 'circuit-established=1' not in response:
       print 'Something is not right.'
    else:
       print 'Looks good.'
    tor_c.close()
except Exception, e:
    print e
    print 'Could not even connect to Tor port!'

This checks if there is an established Tor circuit with GETINFO. If everything works you should see this output:

250 OK
250-status/circuit-established=1
250 OK
250 closing connection

Looks good.

I hope this helps you out.

Your Answer

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

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