import http.server import socketserver import urllib.parse import urllib.request PORT=18001 DATA_SRV_PATH = "/devicedataservice/dataservice" #?data=25e4e40901a2c100000000000000000000413a41040000000000000000001d9222e6 RESPONSES = { 0x24:"A00000000000000001000000000000000000000000000000bec650a1", 0x22:"A20000000000000000000000000000000000000000000000c9950d3f", 0x25:"A00000000000000001000000000000000000000000000000bec650a1", 0x28:"A5000000000000000100000000000000000000000000000056e5abd9", 0x21:"A10000000000000009c4914c0000000000000000000000001d095ec4", 0x29:"" } class Handler(http.server.SimpleHTTPRequestHandler): def do_GET(self): parts = urllib.parse.urlsplit(self.path) if parts.path == DATA_SRV_PATH: self.do_dataservice(parts) def do_dataservice(self, urlparts): params = urllib.parse.parse_qs(urlparts.query) if 'data' not in params: print ('got message without data: ' + urlparts.query) self.send_error(400,'invalid message') data_str = params['data'][0] bin_data = bytes.fromhex(data_str) msg_type = bin_data[0] if msg_type not in RESPONSES: print ('got unknown message: ' + data_str) self.send_error(400,'unknown message') response = RESPONSES[msg_type] self.send_response(http.server.HTTPStatus.OK) self.send_header("Content-type", "text/html") self.send_header("Content-Length", len(response)) self.end_headers() self.wfile.write(response.encode('utf-8')) with socketserver.TCPServer(("",PORT), Handler) as httpd: print('started at port %d'%PORT) httpd.serve_forever()