Last active
August 3, 2021 04:11
-
-
Save congkhoa/c16f819b423d625ee6a20c3f6041473d to your computer and use it in GitHub Desktop.
python3 simple http server print post data
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
#!/usr/bin/env python3 | |
from http.server import HTTPServer, BaseHTTPRequestHandler | |
from io import BytesIO | |
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
print('======= headers =======') | |
print(self.headers.items()) | |
print('\n' * 3) | |
self.send_response(200) | |
self.end_headers() | |
self.wfile.write(b'Hello, world!') | |
def do_POST(self): | |
content_length = int(self.headers['Content-Length']) | |
body = self.rfile.read(content_length) | |
print('======= headers =======') | |
print(self.headers.items()) | |
print('+++++++ body ++++++++++') | |
print(body) | |
print('\n' * 3) | |
self.send_response(200) | |
self.end_headers() | |
response = BytesIO() | |
response.write(body) | |
self.wfile.write(response.getvalue()) | |
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment