背景
我正在尝试为Raspberry Pi Stackexchange 上的问题编写解决方案。此人正在创建一个信息亭,并希望 Midori(Raspbian Wheezy 上的浏览器)在网络断开然后重新连接时重新加载。为了检查互联网连接,我决定让程序尝试连接到 Google。我使用up
根据连接状态分配给 0(向下)或 1(向上)的变量。
我在将新的重新分配up
变量发送到线程时遇到问题。我从我的线程中得到一个永久的“Up has not been reassigned”流。
代码
up = -1 #Undefined state
test_net = ("www.google.com", 80)
#Import modules
import subprocess as sub
from time import sleep
import socket
import threading
def reloader(up):
print "A new thread is born." #Debug
while True:
if up == 1:
print "The system is up."
elif up == 0:
print "The system is down."
#sub.call(["midori", "-e", "Reload"])
else:
print "Up has not been reassigned."
sleep(5)
#Check if internet is up
threading.Thread(target = reloader, args = (up,)).start()
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(test_net)
up = 1
print up
except socket.error:
up = 0
print up
s.close()
sleep(10)