7 Replies - 1093 Views - Last Post: 11 May 2012 - 08:10 AM Rate Topic: -----

#1 dboltz03  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 12
  • Joined: 04-December 11

Running Tor From Python

Posted 07 May 2012 - 09:49 PM

I am trying to write a Python script that runs Tor, waits until it is initialized, and then continues with execution. I am currently using the following code, but it could be quite unreliable depending on how long it takes Tor to initialize.

os.system("tor --quiet &")
time.sleep(7)
print "Done"



I need to start Tor as a background process because if I don't it will hang at the end of initialization. Can anyone point me in the right direction?

Thanks,
Dylan

If anyone is unfamiliar with Tor: https://www.torproject.org/

Is This A Good Question/Topic? 0
  • +

Replies To: Running Tor From Python

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Running Tor From Python

Posted 09 May 2012 - 08:59 AM

that's a tough one. As far as I know, you'd need some way to send a message to Tor saying "are you ready yet?" I don't know of any such thing for Tor though.
Was This Post Helpful? 0
  • +
  • -

#3 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 125
  • Joined: 08-May 11

Re: Running Tor From Python

Posted 10 May 2012 - 01:54 AM

assuming that once tor is running after you enter the command to view the currently running processes on the system, you could check to see if tor is running in the processes and if it is then you know it has been launched and vice versa if it hasn't.
Was This Post Helpful? 0
  • +
  • -

#4 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 125
  • Joined: 08-May 11

Re: Running Tor From Python

Posted 10 May 2012 - 02:34 AM

View Postalexr1090, on 10 May 2012 - 03:54 AM, said:

assuming that once tor is running after you enter the command to view the currently running processes on the system, you could check to see if tor is running in the processes and if it is then you know it has been launched and vice versa if it hasn't.


here's one way to do that. This will work on linux. Just change the ps -A part of the code to whatever the command in windows is to see all of the running processes and it will work there too.

import subprocess
import os
import re
os.system("tor --quiet &")
ps_info = subprocess.check_output(["ps", "-A"])
ps_info = str(ps_info)
if re.search(r'\stor\s', ps_info): print "tor running"
else: print ("tor not running")


Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Running Tor From Python

Posted 10 May 2012 - 08:02 AM

The only problem with that is "running" and "ready" are two very different things.
Was This Post Helpful? 0
  • +
  • -

#6 dboltz03  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 12
  • Joined: 04-December 11

Re: Running Tor From Python

Posted 10 May 2012 - 08:03 AM

Thanks for the response alex, but this wouldn't work. I'm looking for a way to see if Tor has been fully initialized. It's possible for the Tor process to run without the program being fully initialized.
Was This Post Helpful? 0
  • +
  • -

#7 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 125
  • Joined: 08-May 11

Re: Running Tor From Python

Posted 10 May 2012 - 05:38 PM

View Postdboltz03, on 10 May 2012 - 10:03 AM, said:

Thanks for the response alex, but this wouldn't work. I'm looking for a way to see if Tor has been fully initialized. It's possible for the Tor process to run without the program being fully initialized.


Right. Whenever I run that strip of code I wrote earlier it says tor is running and it is as someone pointed out but it's not ready as you want it to be. After it says that tor is running though, I get some messages if it's the first time I'm initilizing tor. The last message I get is "May 10 19:26:45.939 [notice] Tor has successfully opened a circuit. Looks like client functionality is working." Does that mean tor is ready to use? Or if not does tor send any message that says when it's ready?
Was This Post Helpful? 1
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Running Tor From Python

Posted 11 May 2012 - 08:10 AM

you may be on to something there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1