Created
August 23, 2023 21:25
-
-
Save garybake/6c52ea30ea78394a778f1fc23c3c44f8 to your computer and use it in GitHub Desktop.
Simple server that outputs details of the received requests. Used for testing.
This file contains hidden or 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
from http.server import BaseHTTPRequestHandler, HTTPServer | |
class MyHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
self.send_response(200) | |
self.send_header('Content-type', 'text/plain') | |
self.end_headers() | |
self.wfile.write(b'ok') | |
print("=== Request Start ===") | |
print(f"Request method: {self.command}") | |
print(f"Request version: {self.request_version}") | |
print(f"Request path: {self.path}") | |
print(f"Client address: {self.client_address}") | |
print(f"Request headers:") | |
for header, value in self.headers.items(): | |
print(f" {header}: {value}") | |
print("=== Request End ===\n") | |
def do_POST(self): | |
content_length = int(self.headers['Content-Length']) | |
post_data = self.rfile.read(content_length).decode('utf-8') | |
self.send_response(200) | |
self.send_header('Content-type', 'text/plain') | |
self.end_headers() | |
self.wfile.write(b'ok') | |
print("=== Request Start ===") | |
print(f"Request method: {self.command}") | |
print(f"Request version: {self.request_version}") | |
print(f"Request path: {self.path}") | |
print(f"Client address: {self.client_address}") | |
print(f"Request headers:") | |
for header, value in self.headers.items(): | |
print(f" {header}: {value}") | |
print(f"Post data: {post_data}") | |
print("=== Request End ===\n") | |
if __name__ == '__main__': | |
server_address = ('', 8001) | |
httpd = HTTPServer(server_address, MyHandler) | |
print("Running server on port 8001") | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment