I want when I press a button in cherrypy a specific python script is executed I tried to add it like that but of course that doesn't work, What is the right way to do it
What I want to execute is (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())
You have to dd your code before the return
outside the string:
@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>"""
This will send hello
to the serial port. If you want to do it on the button click it has to go into a method called generate
but is similar to index
above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.