简体   繁体   English

通过CGI从python脚本返回值

[英]Return value from python script via CGI

I work on a raspberry Pi. 我在树莓派上工作。 I did a small html page with 2 buttons calling executables via CGI and doing things. 我做了一个小的html页面,带有2个按钮,可通过CGI调用可执行文件并执行操作。 I would like to add a temperature display on the same page. 我想在同一页面上添加温度显示。

I have a small python script that reads the temperature of a sensor and I can successfully display it on console or via direct CGI (typing the url, the only thing displayed is the temperature). 我有一个小的python脚本,可以读取传感器的温度,并且可以在控制台上或通过直接CGI(键入url,显示的唯一内容是温度)成功地将其显示出来。 I would like to see the temperature directly on my html page, and be able to refresh the value by clicking a button. 我想直接在html页面上查看温度,并能够通过单击按钮刷新温度值。

I can't find any answer that might help me returning a value from the python script and displaying it in a textbox or something. 我找不到任何可能帮助我从python脚本返回值并将其显示在文本框等中的答案。 Do you have any track for me to follow ? 你有什么追踪我的路吗?

You can write out a complete html page with CGI 您可以使用CGI写出完整的html页面

A simple CGI script would be 一个简单的CGI脚本是

temp=100
print("Content-Type: text/html")    # HTML is following
print()                             # blank line, end of headers
print("<!doctype html>")
print("<html><head><title>Example</title></head><body>")
print("<p>Temperature is:",temp)
print("</p>")
peint('<a href="#">Reload<a>')
print("</body></html>")

you can also use javascript in a page to (re)load the value with Ajax - there are examples on the page (only replace the php-script with your python-cgi) 您还可以在页面中使用JavaScript来用Ajax (重新)加载值-页面上有示例(仅将php-script替换为python-cgi)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM