#!/usr/bin/env python3 # -*- coding: utf-8 -*- from http.server import BaseHTTPRequestHandler, HTTPServer import json from pprint import pprint hostName = "0.0.0.0" serverPort = 27051 class MyServer(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() print(self.path) self.wfile.write(bytes("https://pythonbasics.org", "utf-8")) self.wfile.write(bytes("

Request: %s

" % self.path, "utf-8")) self.wfile.write(bytes("", "utf-8")) self.wfile.write(bytes("", "utf-8")) def do_POST(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.data_string = self.rfile.read(int(self.headers['Content-Length'])) self.data_string = self.data_string.decode('utf8') try: data = json.loads(self.data_string) pprint(data) except json.decoder.JSONDecodeError as e: data = self.data_string print(data) print() self.wfile.write(bytes("https://pythonbasics.org", "utf-8")) self.wfile.write(bytes("

Request: %s

" % self.path, "utf-8")) self.wfile.write(bytes("", "utf-8")) self.wfile.write(bytes("", "utf-8")) if __name__ == "__main__": webServer = HTTPServer((hostName, serverPort), MyServer) print("Server started http://%s:%s" % (hostName, serverPort)) try: webServer.serve_forever() except KeyboardInterrupt: pass webServer.server_close() print("Server stopped.")