首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用hma的Python子进程

问使用hma的Python子进程
EN

Stack Overflow用户
提问于 2012-07-27 06:00:10
回答 2查看 785关注 0票数 0

我正在尝试使子进程通过linux运行hma代理。我是Python的新手,所以可能我没有使用正确的方法。我需要它做的是在后台运行hma,并让程序检查我的公共IP是否与程序启动前相同,如果不是,则每30分钟重新运行hma程序。

基本上,程序需要检查当前的IP,然后连接到hma。如果第一个IP与第二个IP匹配,即hma尚未连接,则打印等待。如果IP不匹配,则在30分钟后再次运行hma。这是我到目前为止拥有的代码。

代码语言:javascript
复制
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

我做错了什么?如果这是完全错误的,我很抱歉。我是子流程模块的新手

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-27 13:38:09

嗨,所以我不熟悉hma,但像这样的东西应该可以工作。如果没有,dav1d说,确保hma-start在你的路径中。我不太确定为什么要使用/Desktop/hma?当你提升privs时,它在哪里并不重要。

代码语言:javascript
复制
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()
票数 0
EN

Stack Overflow用户

发布于 2012-07-27 06:26:20

如果你正在使用shell=True,你的命令行必须是一个字符串:

代码语言:javascript
复制
hma = 'cd /Desktop/hma; ./hma-start -r'
subprocess.Popen(hma, shell=True)

但是,您也可以在没有shell=True的情况下做到这一点

代码语言:javascript
复制
hma = ['/Desktop/hma/hma-start', '-r']
subprocess.Popen(hma)

如果您想等待进程完成,可以在Popen-Object上调用.communicate()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11678686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档