Skip to content

Instantly share code, notes, and snippets.

@beothorn
Created April 19, 2024 14:17
Show Gist options
  • Save beothorn/a8cf71c533b5d318e6f5283591033f78 to your computer and use it in GitHub Desktop.
Save beothorn/a8cf71c533b5d318e6f5283591033f78 to your computer and use it in GitHub Desktop.
import time
import http.server
import sys
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600)
HOST_NAME = 'localhost' #sys.argv[1]# 'example.net' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 9000 # Maybe set this to 9000.
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write(bytes("<html><head><title>Title goes here.</title></head>".encode("utf-8")))
s.wfile.write(bytes("<body><p>This is a test.</p>".encode("utf-8")))
# If someone went to "http://something.somewhere.net/foo/bar/",
# then s.path equals "/foo/bar/".7
if s.path == '/move':
ser.write(bytes([65]))
s.wfile.write(bytes(("<p>You accessed path: %s</p>" % s.path).encode("utf-8")))
s.wfile.write(bytes("</body></html>".encode("utf-8")))
if _name_ == '_main_':
server_class = http.server.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print(time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment