我不知道如何处理这件事。
我有一个服务器运行在我的Raspberry Pi 3通过Python瓶。这将与HTML文件和CSS文件进行通信。
我希望能够有一个时钟在HTML网页上显示当前的时间和日期,并不断更新它。我已经这样做了(后面显示的代码)。
我面临的主要困境是,我希望时钟在网页上并进行更新,但是需要能够使用PYTHON读取HTML,并根据HTML页面的时间执行一些代码。那么我该怎么做呢?需要将变量从HTML传递到Python吗?谢谢
更新时钟的代码(在HTML文件中):
<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>
<!-- Reference to code for clock: https://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock -->
发布于 2017-06-29 15:13:57
你可以使用php。使用html中的表单,使用php将输入字段的时间值写入文本文件,然后使用python读取输出文件的内容。
您的.py文件看起来应该是这样的(理想情况下,您的时间变量应该是一个变量、小时、秒等,但是您可能有更多,所以我已经放了一个循环)。
from time import sleep
def read():
timevars = ['your variables here']
while True:
file=open("timedata.txt","r")
data = file.read()
for i in range(len(timevars)):
if data == inlist[i]:
print(data)
file=open("timedata.txt","w")
file.write("")
file.close()
sleep(0.01)
read() #you will be able to see the vars in the python file now, but you
#need to adjust so it only prints out the most recent valuephp已经足够基本了--只要打开文件,就可以写出来。
<?php
$name = $_POST['writetime'];
$fptime = fopen("timedata.txt", "w");
fwrite($fptime, "");
$savestring = $name;
fwrite($fptime, $savestring);
fclose($fptime);
?>https://stackoverflow.com/questions/44824877
复制相似问题