我想当我在cherrypy中按下一个按钮时,执行一个特定的python脚本,我试图这样添加它,但这当然不起作用,正确的方法是什么?
我要执行的是(ser=serial.serial('/dev/ttyusb0')
先生。写(b'hello'))
import cherrypy
import string
class HelloWorld:
""" Sample request handler class. """
@cherrypy.expose
def index(self):
return """<html>
<head></head>
<body>
<form method="get" action="generate">
<button type="submit">Press!</button>
</form>
</body>
ser = serial.Serial('/dev/ttyUSB0')
ser.write(b'hello')
</html>"""
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0'} )
cherrypy.quickstart(HelloWorld())
最佳答案
必须在字符串外部的return
之前添加代码:
@cherrypy.expose
def index(self):
ser = serial.Serial('/dev/ttyUSB0')
ser.write(b'hello')
return """<html>
<head></head>
<body>
<form method="get" action="generate">
<button type="submit">Press!</button>
</form>
</body>
</html>"""
这将把
hello
发送到串行端口。如果要在按钮上执行此操作,单击它必须进入一个名为generate
的方法,但与上面的index
类似。关于python - Cherrypy服务器运行python脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35942081/