从web界面与Python脚本交互的一个简单的web服务器方法是使用Python瓶。在
下面是一个基本的Python bottle程序,可用于您的目的:from bottle import route, run
@route('/')
def hello():
#using jquery
return """
temp will update here
"""
@route('/get_temp')
def getTemp():
temp = readDataBaseForTemp()
return temp
run(host='localhost', port=8080, debug=True)
启动此程序时,您可以使用浏览器在http://localhost:8080/<;上与它交互-这将触发JavaScript,该JavaScript将轮询服务器以获取temp。显然,这还不完整,但这是总体思路。在
这里的想法是JavaScript只调用pythonwebserver(使用URLhttp://localhost:8080/get_temp),它触发pythonmytemperaturecontrol脚本。当你的脚本执行完毕并返回一个温度值时,它将把数据发送回请求它的JavaScript,这样网页就可以相应地更新了。在
至于你的我的温度控制.py脚本您可以将温度读数的输出发送到Web服务器可以访问的公共位置。通常情况下,您会为此设置一个数据库。在
^{pr2}$