Last active
July 20, 2023 11:56
-
-
Save harshithjv/56595f18e6b57e6b5112b9167730b57a to your computer and use it in GitHub Desktop.
A small Python script to start a server to handle both GET and POST requests.
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
| # Ref: https://stackoverflow.com/a/66514587 | |
| # Just made changes reponse to JSON type | |
| from http.server import BaseHTTPRequestHandler, HTTPServer | |
| import json | |
| class handler(BaseHTTPRequestHandler): | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| def do_GET(self): | |
| self.send_response(200) | |
| self.send_header('Content-type','application/json') | |
| self.end_headers() | |
| message = json.dumps({"message": "Hello, World! Here is a GET response"}) | |
| self.wfile.write(message.encode(encoding='utf_8')) | |
| def do_POST(self): | |
| self.send_response(200) | |
| self.send_header('Content-type','application/json') | |
| self.end_headers() | |
| message = json.dumps({"message": "Hello, World! Here is a POST response"}) | |
| self.wfile.write(message.encode(encoding='utf_8')) | |
| with HTTPServer(('0.0.0.0', 8050), handler) as server: | |
| server.serve_forever() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment