Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Created June 6, 2021 07:48
Show Gist options
  • Save FerdinaKusumah/cfcb60b798c5e231c0549df5d8f29d57 to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/cfcb60b798c5e231c0549df5d8f29d57 to your computer and use it in GitHub Desktop.
Python Simple Http Server
import json
import http.server
import socketserver
from typing import Tuple
from http import HTTPStatus
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, request: bytes, client_address: Tuple[str, int], server: socketserver.BaseServer):
super().__init__(request, client_address, server)
@property
def api_response(self):
return json.dumps({"message": "Hello world"}).encode()
def do_GET(self):
if self.path == '/':
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(bytes(self.api_response))
if __name__ == "__main__":
PORT = 8000
# Create an object of the above class
my_server = socketserver.TCPServer(("0.0.0.0", PORT), Handler)
# Star the server
print(f"Server started at {PORT}")
my_server.serve_forever()
"""
To test api doing curl
→ curl http://localhost:8000
{"message": "Hello world"}%
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment