forked from Orange-OpenSource/python-ngsild-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_alert_receiver_server.py
62 lines (52 loc) · 1.72 KB
/
simple_alert_receiver_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# Software Name: ngsildclient
# SPDX-FileCopyrightText: Copyright (c) 2021 Orange
# SPDX-License-Identifier: Apache 2.0
#
# This software is distributed under the Apache 2.0;
# see the NOTICE file for more details.
#
# Author: Fabien BATTELLO <[email protected]> et al.
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
PORT = 8000
ENDPOINT = "/air_quality_alerts"
class CustomHandler(BaseHTTPRequestHandler):
def _response(self, status: int, msg: str):
self.send_response(status)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(msg.encode())
def do_POST(self):
if not self.path.startswith(ENDPOINT):
self.log_error("Wrong endpoint")
self._response(404, "Wrong endpoint")
return
if self.headers.get("Content-Type") != "application/ld+json":
self.log_error("JSON-LD expected")
self._response(415, "JSON-LD expected")
return
length = int(self.headers["Content-Length"])
content = self.rfile.read(length)
self.log_message(f"Notification received :\n{content.decode('utf-8')}")
self._response(200, "OK")
def main():
argc, argv = len(sys.argv), sys.argv
port: int = PORT
if argc > 2:
sys.exit(1)
elif argc == 2:
try:
port = int(argv[1])
except ValueError:
sys.exit(2)
httpd = HTTPServer(("0.0.0.0", port), CustomHandler)
httpd.allow_reuse_address = True
print("Start HTTP Server")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
print("\nStop HTTP Server")
if __name__ == "__main__":
main()