Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 每X秒运行一次Python脚本_Jquery_Python_Html - Fatal编程技术网

Jquery 每X秒运行一次Python脚本

Jquery 每X秒运行一次Python脚本,jquery,python,html,Jquery,Python,Html,我有一个Python脚本,它在引导时运行良好,可以检测wifi连接的状态,并根据脚本结果将HTML IMG标记写入文件 我希望它“持续”运行,我知道这可以通过使用CRON作业来实现,但脚本最频繁的运行时间是1分钟,我希望在结果更改后的几秒钟内知道 我尝试了许多贝娄代码的变体,但它似乎从未运行过。(我删除了WiFi加密狗,它应该会更改。如果我删除它并重新启动,则会显示正确的结果。) PYTHON: import time, urllib2 def internet_on(): try:

我有一个Python脚本,它在引导时运行良好,可以检测wifi连接的状态,并根据脚本结果将HTML IMG标记写入文件

我希望它“持续”运行,我知道这可以通过使用CRON作业来实现,但脚本最频繁的运行时间是1分钟,我希望在结果更改后的几秒钟内知道

我尝试了许多贝娄代码的变体,但它似乎从未运行过。(我删除了WiFi加密狗,它应该会更改。如果我删除它并重新启动,则会显示正确的结果。)

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()
<!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>
导入时间,urllib2 定义internet_on(): 尝试: response=urllib2.urlopen('http://64.233.160.94,超时=1) 返回“” 除了urllib2.URLError作为err:pass 返回“” 输出=internet_on() f=open('/var/www/html/viv/wifiout.html','w') 打印>>f,输出 f、 关闭() 时间。睡眠(1) 尽管如此: 互联网() HTML:

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()
<!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>

维瓦里姆环境控制中心
函数updateTime(){
var currentTime=新日期();
var hours=currentTime.getHours();
var minutes=currentTime.getMinutes();
var seconds=currentTime.getSeconds();
如果(分钟<10){
分钟=“0”+分钟;
}
如果(秒<10){
秒=“0”+秒;
}
变量v=小时+“:“+分钟+”:“+秒+”;
如果(小时数>11){
v+=“PM”;
}否则{
v+=“AM”
}
setTimeout(“updateTime()”,1000);
document.getElementById('time').innerHTML=v;
}
$(“文档”).ready(函数(){
updateTime();
setInterval(函数(){
$(“#wifi”).load('wifiout.html');
},1000);
});
函数changeStatus(){
var image=document.getElementById('lightStatus');
if(image.src.match(“lightoff”)){
image.src=“lighton.png”;
}否则{
image.src=“lightoff.png”;
}
}
应用接受的答案运行一段时间后引发的错误

pi@Vivarium:~$sudo python/home/pi/Desktop/wifi.py 回溯(最近一次呼叫最后一次): 文件“/home/pi/Desktop/wifi.py”,第17行,在 互联网() 文件“/home/pi/Desktop/wifi.py”,第8行,在internet上 urllib2.urlopen(“”,超时=1) 文件“/usr/lib/python2.7/urllib2.py”,第154行,在urlopen中 返回opener.open(url、数据、超时) 文件“/usr/lib/python2.7/urllib2.py”,第437行,打开 响应=方法(请求,响应) http_响应中的文件“/usr/lib/python2.7/urllib2.py”,第550行 “http”、请求、响应、代码、消息、hdrs) 文件“/usr/lib/python2.7/urllib2.py”,第469行出错 结果=自身调用链(*args) 文件“/usr/lib/python2.7/urllib2.py”,第409行,在调用链中 结果=func(*args) http_error_302中的文件“/usr/lib/python2.7/urllib2.py”,第656行 返回self.parent.open(新建,超时=请求超时) 文件“/usr/lib/python2.7/urllib2.py”,第437行,打开 响应=方法(请求,响应) http_响应中的文件“/usr/lib/python2.7/urllib2.py”,第550行 “http”、请求、响应、代码、消息、hdrs) 文件“/usr/lib/python2.7/urllib2.py”,第469行出错 结果=自身调用链(*args) 文件“/usr/lib/python2.7/urllib2.py”,第409行,在调用链中 结果=func(*args) http_error_302中的文件“/usr/lib/python2.7/urllib2.py”,第656行 返回self.parent.open(新建,超时=请求超时) 文件“/usr/lib/python2.7/urllib2.py”,第431行,打开 响应=自身打开(请求,数据) 文件“/usr/lib/python2.7/urllib2.py”,第449行,打开 "开放", 文件“/usr/lib/python2.7/urllib2.py”,第409行,在调用链中 结果=func(*args) http\u open中的文件“/usr/lib/python2.7/urllib2.py”,第1227行 返回self.do_open(httplib.HTTPConnection,req) 文件“/usr/lib/python2.7/urllib2.py”,第1200行,打开 r=h.getresponse(缓冲=True) 文件“/usr/lib/python2.7/httplib.py”,第1073行,在getresponse中 response.begin() 文件“/usr/lib/python2.7/httplib.py”,第415行,在begin中 版本、状态、原因=self.\u读取\u状态() 文件“/usr/lib/python2.7/httplib.py”,第371行,处于读取状态 line=self.fp.readline(_MAXLINE+1) 文件“/usr/lib/python2.7/socket.py”,第476行,在readline中 数据=self.\u sock.recv(self.\r bufsize) socket.timeout:超时


我认为您的问题在于您没有在循环中编写新的HTML,在最初运行脚本时只编写一次。试试这样的

import time, urllib2

HTML = '<img class="right" src="{}" width="32" height="32">'

def internet_on():

    try:
        urllib2.urlopen('http://64.233.160.94',timeout=1)
        status_image = 'networkon.png'
    except urllib2.URLError as err:
        status_image = 'networkoff.png'

    with open('/var/www/html/viv/wifiout.html', 'w') as f:
        f.write(HTML.format(status_image))

while True:
    internet_on()
    time.sleep(1)
导入时间,urllib2 HTML=“” 定义internet_on(): 尝试: urllib2.urlopen('http://64.233.160.94,超时=1) 状态\图像='networkon.png' 除urllib2.URLError作为错误外: 状态\图像='networkoff.png' 将open('/var/www/html/viv/wifiout.html',w')作为f: f、 写入(HTML.format(状态\图像)) 尽管如此: 互联网() 时间。睡眠(1)
我认为您的问题在于您没有在循环中编写新的HTML,在最初运行脚本时只编写一次。试试这样的

import time, urllib2

HTML = '<img class="right" src="{}" width="32" height="32">'

def internet_on():

    try:
        urllib2.urlopen('http://64.233.160.94',timeout=1)
        status_image = 'networkon.png'
    except urllib2.URLError as err:
        status_image = 'networkoff.png'

    with open('/var/www/html/viv/wifiout.html', 'w') as f:
        f.write(HTML.format(status_image))

while True:
    internet_on()
    time.sleep(1)
导入时间,urllib2 HTML=“” 定义internet_on(): 尝试: urllib2.urlopen('http://64.233.160.94,超时=1) 状态\图像='networkon.png' 除urllib2.URLError作为错误外: 状态\图像='networkoff.png' 将open('/var/www/html/viv/wifiout.html',w')作为f: f、 写入(HTML.format(状态\图像)) 尽管如此: 互联网() 时间。睡眠(1)
是的,您只运行代码的后半部分(从分配输出到时间)。睡眠(1)第一次,其余的时间都被忽略了,因为它不在函数定义下。将所有代码移动到函数下。无需返回任何内容,只需在函数中执行即可。时间。睡眠(1)仅处于启用状态