Browse Source

Schalten der LED per URL-Request

develop
Olli Graf 2 years ago
parent
commit
63210ee88d
  1. 1
      gpio/.gitignore
  2. 1
      gpio/ledserver.log
  3. 50
      gpio/ledserver.py

1
gpio/.gitignore

@ -1,2 +1,3 @@
./__pycache__ ./__pycache__
*.log
./network/__pycache__/* ./network/__pycache__/*

1
gpio/ledserver.log

@ -1 +0,0 @@
2023-04-29 10:49:08,638 [DEBUG] <module>: MAIN: start

50
gpio/ledserver.py

@ -1,32 +1,46 @@
#! /usr/bin/python3 #! /usr/bin/python3
from http.server import BaseHTTPRequestHandler,HTTPServer from http.server import SimpleHTTPRequestHandler
import socketserver
import logging import logging
from led import setLED
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', filename='ledserver.log', level=logging.DEBUG)
class http_server:
def __init__(self):
def handler(*args):
myHandler(*args)
server = HTTPServer(('', 8080), handler)
class myHandler(BaseHTTPRequestHandler): logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
def __init__(self,*args):
self.t1 = t1
BaseHTTPRequestHandler.__init__(self, *args)
class LEDHttpRequestHandler(SimpleHTTPRequestHandler):
def do_GET(self): def do_GET(self):
logging.debug(f'path= {self.path}')
logging.debug('GET: empfangen') logging.debug('GET: empfangen')
logging.debug(f'Request: {self.request}')
self.send_response(200) self.send_response(200)
self.send_header('Content-type','text/html') self.send_header('Content-Type:','text/html')
self.end_headers()
self.wfile.write(self.t1.show()) #Doesnt work if 'led=on' in self.path:
return logging.debug('schalte LED ein')
setLED(True)
else:
logging.debug('schalte LED aus')
setLED(False)
self.wfile.write('<p>LED geschaltet</p>'.encode())
class main:
class http_server:
def __init__(self): def __init__(self):
def handler(*args):
myHandler(*args)
server = HTTPServer(('', 8080), handler)
self.server = http_server()
if __name__ == '__main__': if __name__ == '__main__':
logging.debug('MAIN: start') logging.debug('MAIN: start')
main() handler = LEDHttpRequestHandler
with socketserver.TCPServer(('',9000),handler) as httpd:
logging.info('SERVER: start')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass

Loading…
Cancel
Save