Created
May 20, 2023 06:45
-
-
Save hollyhockberry/432579081dfb35cb36962650c9f5a0d8 to your computer and use it in GitHub Desktop.
py: Show POST request header and body.
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 | |
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