Python Linux IsAlive Midori看门狗

2024-04-26 21:06:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现一个看门狗,它将ping一个主机,如果它启动了,它将让midori打开远程页面,如果没有,那么它将打开一个本地页面。在

我从这里的代码改编而来:https://raspberrypi.stackexchange.com/questions/6981/auto-refresh-for-midori。在

问题是,对于每个情况,它似乎只工作一次,例如,如果远程站点启动,则显示远程页面,当它关闭时,显示本地页面,但当远程再次上升时,midori不加载页面。在

我已经禁用了midori调用,只输出了up和last vars,它们是正确的,if条件也正确执行,所以看起来它与popen和次级股. 在

有什么想法吗??在

#!/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)

似乎每次副总裁()是一个新的进程启动,直到有2个进程,然后什么都没有:

^{pr2}$

Tags: toimportlocalhostif远程time页面socket
1条回答
网友
1楼 · 发布于 2024-04-26 21:06:48

subprocess call等待命令完成。在

所以感觉midori的电话没有回音。当它被召唤时,它会怎么做?它会在一段时间后自动关闭,从而将流返回到Python代码吗?在

尝试执行:

sub.call(["midori", "-a", "localhost:888","-e","Fullscreen"])

在Python解释器中,检查它是否返回。在

如果它没有返回,您应该尝试一下来自subprocess模块的Popen。将midori的东西打包到Popen构造中,(也许)等待一些特定的输出和结束midori(或者,如果可以将新的url传递给正在运行的midori,则保持它的活动状态)。在

相关问题 更多 >