Parcourir la source

basic http server

Guilhem Saurel il y a 6 ans
Parent
commit
781e4fd78c
1 fichiers modifiés avec 35 ajouts et 0 suppressions
  1. 35 0
      main.py

+ 35 - 0
main.py

@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+"""
+wifi-with-matrix script.
+Bridge between https://code.ffdn.org/FFDN/wifi-with-me & a matrix room
+"""
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+
+SERVER_ADDRESS = ('', 4785)
+
+
+class Forwarder(BaseHTTPRequestHandler):
+    """
+    Class given to the server, st. it knows what to do with a request.
+    This one handles the HTTP request, and forwards it to the matrix room.
+    """
+
+    def do_POST(self):
+        """
+        main method, get a json dict from wifi-with-me, send a message to a matrix room
+        """
+        self.ret_ok()
+
+    def ret_ok(self):
+        """
+        return a success status
+        """
+        self.send_response(200)
+        self.send_header('Content-Type', 'application/json')
+        self.end_headers()
+        self.wfile.write(b"{'status': 'OK'}")
+
+
+if __name__ == '__main__':
+    HTTPServer(SERVER_ADDRESS, Forwarder).serve_forever()