Skip to content

Instantly share code, notes, and snippets.

@harshithjv
Last active July 20, 2023 11:56
Show Gist options
  • Select an option

  • Save harshithjv/56595f18e6b57e6b5112b9167730b57a to your computer and use it in GitHub Desktop.

Select an option

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.
# 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