我正在尝试使子进程通过linux运行hma代理。我是Python的新手,所以可能我没有使用正确的方法。我需要它做的是在后台运行hma,并让程序检查我的公共IP是否与程序启动前相同,如果不是,则每30分钟重新运行hma程序。
基本上,程序需要检查当前的IP,然后连接到hma。如果第一个IP与第二个IP匹配,即hma尚未连接,则打印等待。如果IP不匹配,则在30分钟后再次运行hma。这是我到目前为止拥有的代码。
import os
import webbrowser
import time
import socket
import urllib2
import subprocess
response = urllib2.urlopen("http://automation.whatismyip.com/n09230945.asp")
internal = response.read()
print "Internal IP Address is ", internal
hma = ['cd', '/Desktop/hma', ';', './hma-start', '-r']
subprocess.Popen(hma, shell=True)
response = urllib2.urlopen("http://automation.whatismyip.com/n09230945.asp")
external = response.read()
while (internal == external):
time.sleep(1)
response = urllib2.urlopen("http://automation.whatismyip.com/n09230945.asp")
external = response.read()
print 'waiting'
while (internal != external):
print 'It changed'
hma = ['cd', '/Desktop/hma', ';', './hma-start', '-r']
subprocess.Popen(hma, shell=True)
response = urllib2.urlopen("http://automation.whatismyip.com/n09230945.asp")
external = response.read()
print "External IP Address is ", external我做错了什么?如果这是完全错误的,我很抱歉。我是子流程模块的新手
发布于 2012-07-27 13:38:09
嗨,所以我不熟悉hma,但像这样的东西应该可以工作。如果没有,dav1d说,确保hma-start在你的路径中。我不太确定为什么要使用/Desktop/hma?当你提升privs时,它在哪里并不重要。
import os
import webbrowser
import time
import socket
import urllib2
import subprocess
import socket
URL = "http://automation.whatismyip.com/n09230945.asp"
DIR = '/Desktop/hma'
HMA = ['./hma-start', '-r']
WAIT_TIME = 60 * 30 # 30 min
GET_IP = lambda: urllib2.urlopen(URL).read()
if __name__ == '__main__':
external = internal = GET_IP()
print "Internal IP Address is %s" % internal
try:
os.chdir(DIR)
except OSError:
print "%s not found" % DIR
print "External IP Address is ", external
while True:
external = GET_IP()
if external != internal:
print "Proxied"
time.sleep(WAIT_TIME)
else:
print "Not Proxied"
proc = subprocess.Popen(HMA)
proc.wait()发布于 2012-07-27 06:26:20
如果你正在使用shell=True,你的命令行必须是一个字符串:
hma = 'cd /Desktop/hma; ./hma-start -r'
subprocess.Popen(hma, shell=True)但是,您也可以在没有shell=True的情况下做到这一点
hma = ['/Desktop/hma/hma-start', '-r']
subprocess.Popen(hma)如果您想等待进程完成,可以在Popen-Object上调用.communicate()。
https://stackoverflow.com/questions/11678686
复制相似问题