Skip to content

Instantly share code, notes, and snippets.

@hollyhockberry
Created May 20, 2023 06:45
Show Gist options
  • Save hollyhockberry/432579081dfb35cb36962650c9f5a0d8 to your computer and use it in GitHub Desktop.
Save hollyhockberry/432579081dfb35cb36962650c9f5a0d8 to your computer and use it in GitHub Desktop.
py: Show POST request header and body.
import http.server
class handler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
""" show request header """
print()
print('=== POST REQUEST HEADER ===')
print(self.headers)
content_length = self.headers.get("content-length")
if content_length:
print('=== POST REQUEST BODY ===')
length = int(content_length)
body = self.rfile.read(length)
if length < 600:
print(body)
else:
print(body[:300])
print(' :')
print(body[-300:])
print('=== END ===')
""" send response """
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
if __name__ == '__main__':
httpd = http.server.HTTPServer(('0.0.0.0', 80), handler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment