Python CGI与串口通信
问题描述:
我在通过python cgi程序与串口通信时遇到问题。我在html中有两个按钮来控制两个LED(LED1,LED2)。Python CGI与串口通信
<html>
<h1>Test Page 2</h1>
<form name="input" action="/cgi-bin/myscript-2.py" method="get">
<input type="submit" value="LED1" name="Submit">
<input type="submit" value="LED2" name="Submit1">
</form>
</html>
现在我的Python程序从这里接收结果
#!/usr/bin/python
import cgi
import cgitb;
import serial
form = cgi.FieldStorage()
cgitb.enable()
port = "/dev/ttyACM0"
ser = serial.Serial(port, 9600, timeout=0)
if "LED1" in form:
ser.write("hello")
elif "LED2" in form:
ser.write("hello")
else:
print "Couldn't determine which button was pressed."
我总是让想不出确定button.The Apache的错误日志是这样
[Sat Nov 08 12:41:41.579738 2014] [cgid:error] [pid 1080:tid 3004140352] [client 127.0.0.1:48698] malformed header from script 'myscript-2.py': Bad header: Couldn't determine which butto, referer: http://localhost/page2.html
答
如果使用CGI的脚本您必须先打印标题。
看看https://wiki.python.org/moin/CgiScripts。 你缺少
print "Content-type: text/html"
print
为脚本的第一个语句。
当您的代码包含“无法确定哪个按钮被按下”时,您是否真的得到了错误消息“无法确定哪个按钮”(最后没有“n”)? – 2014-11-08 21:12:18
另外,你有没有尝试简单地打印“表格”?价值最终成为什么? – 2014-11-08 21:13:01