[英]Cherrypy server run a python script
我想當我在cherrypy中按下一個按鈕時,執行了一個特定的python腳本,我試圖像那樣添加它,但是當然不起作用,正確的方法是什么
我要執行的是(ser = serial.Serial('/ dev / ttyUSB0')ser.write(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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.