Skip to content

Instantly share code, notes, and snippets.

@congkhoa
Last active August 3, 2021 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save congkhoa/c16f819b423d625ee6a20c3f6041473d to your computer and use it in GitHub Desktop.
Save congkhoa/c16f819b423d625ee6a20c3f6041473d to your computer and use it in GitHub Desktop.
python3 simple http server print post data
#!/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