-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" Generic Telemetry web service """ | ||
|
||
from http.server import BaseHTTPRequestHandler, HTTPServer, ThreadingHTTPServer | ||
import json | ||
from socketserver import BaseRequestHandler | ||
import time | ||
import threading | ||
from typing import Callable, Tuple | ||
|
||
class TelemetryServer(ThreadingHTTPServer): | ||
data = {} | ||
|
||
def __init__(self, ip, port): | ||
super().__init__((ip, port), TelemetryHandler) | ||
|
||
|
||
def set_data(self, data_obj): | ||
self.data = data_obj | ||
|
||
def run(self): | ||
server_thread = threading.Thread(target=self.serve_forever) | ||
server_thread.daemon_threads = True | ||
server_thread.start() | ||
|
||
class TelemetryHandler(BaseHTTPRequestHandler): | ||
|
||
def do_GET(self): | ||
self.send_response(200) | ||
self.send_header('Content-type', 'application/json') | ||
self.end_headers() | ||
|
||
self.wfile.write(bytes(json.dumps(self.server.data), "utf-8")) | ||
self.wfile.flush() | ||
|
||
|
||
if __name__ == "__main__": | ||
server = TelemetryServer('127.0.0.1', 5000) | ||
server.run() | ||
|
||
print("server started") | ||
server.set_data({'hello': 'world', 'received': 'ok'}) | ||
|
||
|
||
input("Press any key to terminate the program") | ||
server.set_data({'hello': 'world', 'received': 'err'}) | ||
input("Press any key to terminate the program") | ||
|
||
server.shutdown() | ||
print("Server stopped.") | ||
|