我正在尝试实现一个看门狗,它将ping一个主机,如果它启动了,它将让midori打开远程页面,如果没有,它将打开一个本地页面。
我从这里的代码中改编了这个:https://raspberrypi.stackexchange.com/questions/6981/auto-refresh-for-midori。
问题是,对于每种情况,它似乎只工作一次,例如,如果远程站点启动,则显示远程页面,当它关闭时,显示本地页面,但当远程再次启动时,midori不加载页面。
我已经禁用了midori调用,只输出了up和last变量,它们是正确的,if条件也正确执行,所以看起来它与popen和sub.calls有关。
你知道这个问题是什么吗??
#!/usr/bin/env python
host = "localhost"
port = 888
recheck_time = 10
page_to_open_to = "/"
lurl = "///usr/share/scripts/splash.htm"
last = -1 #undefined state
up = -1 #Undefined state
import subprocess as sub
from time import sleep
import socket
import threading
sub.Popen(["midori", "-a","localhost:888","-e","Fullscreen"]) #open midori
#Check if internet is up
addr = (host, port) #the connection addr
while True:
last = up #reset checking var
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create socket
try: #attempt to ping, change vars
s.connect(addr)
up = 1
except socket.error:
up = 0
if up == 1 and last == 0:
sub.call(["midori", "-a", "localhost:888","-e","Fullscreen"])
print('up')
elif up == 0 and last == 1:
sub.call(["midori", "-a",lurl,"-e","Fullscreen"])
print("down")
s.close()
print(up,",",last)
sleep(recheck_time)似乎每次执行sub.call()都会启动一个新进程,直到有两个进程,然后就什么都没有了:
root 3499 0.3 0.5 10792 5856 tty1 S 17:44 0:00 /usr/bin/python /usr/share/scripts/midori.py
root 3500 3.3 4.7 191620 48240 tty1 Sl 17:44 0:03 midori -a localhost:888 -e Fullscreen
root 3530 1.0 2.8 173732 28836 tty1 Sl 17:45 0:00 midori -a ///usr/share/scripts/splash.htm发布于 2013-09-05 17:13:39
subprocess call等待命令完成。
因此,感觉midori调用没有返回。当midori被调用时,它会做什么?它会在一段时间后自动关闭,从而将流返回到Python代码吗?
尝试执行以下命令:
sub.call(["midori", "-a", "localhost:888","-e","Fullscreen"])在Python解释器中,检查它是否返回。
如果没有返回,您应该从子进程模块中尝试Popen。将midori内容打包到一个Popen结构中,(可能)等待一些特定的输出和end midori (或者如果您可以将新的URL传递给正在运行的midori,则让它保持活动状态)。
https://stackoverflow.com/questions/18630061
复制相似问题