簡體   English   中英

Cherrypy服務器運行python腳本

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM