import http.server from typing import Optional class EchoRequestHandler(http.server.BaseHTTPRequestHandler): def _log_request(self, body: Optional[str] = None): print(f"Received {self.command} request from {self.client_address}") print(f"Path: {self.path}") if body: print(f"Body: {body}") def _send_response(self, message): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() self.wfile.write(message.encode()) def get_body(self): content_length = self.headers.get('Content-Length') if content_length: content_length = int(content_length) body = self.rfile.read(content_length).decode('utf-8') return body return None def do_GET(self): self._log_request(self.get_body()) self._send_response(f"Echo: GET {self.path}") def do_POST(self): body = self.get_body() self._log_request(body) self._send_response(f"Echo: POST {self.path} with body: {body}") if __name__ == "__main__": server_address = ('', 8080) httpd = http.server.HTTPServer(server_address, EchoRequestHandler) print(f"Server address: {httpd.server_address}") print("Starting server on port 8080...") httpd.serve_forever()