gpt4 book ai didi

Python 套接字超时

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:53 24 4
gpt4 key购买 nike

我在启动时运行一个用 Python 编写的脚本,并且每秒持续运行。它在有限的时间内工作正常(似乎变化约 5/10 分钟),然后产生以下错误。

我没有尝试更改 sleep 时间,因为这是一个“测试”脚本,但我将使用必须每秒运行的脚本,因此试图找到正确的方法来做到这一点。

Google 似乎没有提供多少答案,也许我使用了错误的术语,但我不确定它的 sleep 时间是否可能是 ping IP 失败?

Python

import time, urllib2

def internet_on():
try:
response=urllib2.urlopen('http://64.233.160.94',timeout=1)
return '<img class="right" src="networkon.png" width="32" height="32">'
except urllib2.URLError as err: pass
return '<img class="right" src="networkoff.png" width="32" height="32">'

output = internet_on()
f = open('/var/www/html/viv/wifiout.html', 'w')
print >> f, output
f.close()

time.sleep(1)

while True:
internet_on()

HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vivarium Enviroment Control Centre</title>
<link rel="stylesheet" href="style.css">
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
function updateTime() {
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
if (minutes < 10){
minutes = "0" + minutes;
}
if (seconds < 10){
seconds = "0" + seconds;
}
var v = hours + ":" + minutes + ":" + seconds + " ";
if(hours > 11){
v+="PM";
} else {
v+="AM"
}
setTimeout("updateTime()",1000);
document.getElementById('time').innerHTML=v;
}

$("document").ready(function(){
updateTime();

setInterval(function(){
$("#wifi").load('wifiout.html');
},1000);
});

function changeStatus() {
var image = document.getElementById('lightStatus');
if (image.src.match("lightoff")) {
image.src = "lighton.png";
} else {
image.src = "lightoff.png";
}
}
</script>
</head>
<body>
<div id="topbar">
<span id="time"></span>
<span id="wifi"></span>
<img id="lightStatus" class="right" onclick="changeStatus()" src="lightoff.png" width="32" height="32">
</div>
</body>
</html>

运行一段时间后抛出错误

pi@Vivarium:~ $ sudo python /home/pi/Desktop/wifi.py
Traceback (most recent call last):
File "/home/pi/Desktop/wifi.py", line 17, in <module>
internet_on()
File "/home/pi/Desktop/wifi.py", line 8, in internet_on
urllib2.urlopen('http://64.233.160.94',timeout=1)
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 469, in error
result = self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 656, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/usr/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 469, in error
result = self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 656, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/usr/lib/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1200, in do_open
r = h.getresponse(buffering=True)
File "/usr/lib/python2.7/httplib.py", line 1073, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 415, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 371, in _read_status
line = self.fp.readline(_MAXLINE + 1)
File "/usr/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
socket.timeout: timed out

最佳答案

试试这个:

import time, urllib2

def internet_on():
returnValue = '<img class="right" src="networkon.png" width="32" height="32">'
try:
response=urllib2.urlopen('http://64.233.160.94',timeout=1)
except:
returnValue = '<img class="right" src="networkoff.png" width="32" height="32">'
return returnValue


while True:
output = internet_on()
with open('/var/www/html/viv/wifiout.html', 'w') as f:
f.write(output)
time.sleep(5)

关于Python 套接字超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36408289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com