網頁

2017年3月22日 星期三

Python 的 SimpleHTTPServer

參考 Arduino Yún 小試(2)透過瀏覽器點光明燈

vi lightUpL13Http.py
import sys
import SimpleHTTPServer
import SocketServer
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient

class LedRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/ledOn':
            BridgeClient().put('state', '1')
        elif self.path == '/ledOff':
            BridgeClient().put('state', '0')
        self.path = '/led.html'
        return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

server = SocketServer.TCPServer(('0.0.0.0', 8000), LedRequestHandler)
server.serve_forever()

vi led.html
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title></title>
  </head>
  <body>
     <p><a href="ledOn">On</a>&nbsp; <a href="ledOff">Off</a></p>
  </body>
</html>

python lightUpL13Http.py

http://arduino.local:8000/led.html


沒有留言:

張貼留言