Created
April 13, 2023 13:13
-
-
Save hugotkk/e926c0eeef944de0fe824107199ba0f8 to your computer and use it in GitHub Desktop.
Playground - AWS API Gateway
This file contains 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
import http.server | |
import json | |
from urllib.parse import urlparse, parse_qs | |
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler): | |
def do_POST(self): | |
self.do_GET() | |
def do_GET(self): | |
parsed_url = urlparse(self.path) | |
query_string = parse_qs(parsed_url.query) | |
path_info = parsed_url.path | |
headers = {} | |
for key, value in self.headers.items(): | |
headers[key] = value | |
payload = '' | |
if self.headers.get('Content-Length'): | |
content_length = int(self.headers.get('Content-Length')) | |
payload = self.rfile.read(content_length).decode('utf-8') | |
response_data = { | |
'headers': headers, | |
'payload': payload, | |
'query_string': query_string, | |
'path_info': path_info, | |
} | |
response_json = json.dumps(response_data).encode('utf-8') | |
self.send_response(200) | |
self.send_header('Content-type', 'application/json') | |
self.send_header('Content-length', len(response_json)) | |
self.end_headers() | |
self.wfile.write(response_json) | |
if __name__ == '__main__': | |
PORT = 80 | |
Handler = MyHttpRequestHandler | |
with http.server.HTTPServer(("", PORT), Handler) as httpd: | |
print(f"Server started at localhost:{PORT}") | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment