-
-
Save apfelchips/5d7b772a23139b8dd14f12b882b561d5 to your computer and use it in GitHub Desktop.
simple python http server to dump request headers
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 | |
""" | |
inspiration: | |
https://gist.github.com/phrawzty/62540f146ee5e74ea1ab?permalink_comment_id=2358615 | |
https://gist.github.com/nitaku/10d0662536f37a087e1b#file-server-py | |
example: | |
curl -s -H "X-Something: yeah" localhost:8080 | |
curl --data "{\"this\":\"is a test\"}" localhost:8080 | |
""" | |
from http.server import SimpleHTTPRequestHandler | |
from socketserver import TCPServer | |
# from rich import * | |
import socket | |
import sys | |
# import cgi | |
# import json | |
# import logging | |
try: | |
PORT = int(sys.argv[1]) | |
except: | |
PORT = 8080 | |
class CustomHandler(SimpleHTTPRequestHandler): | |
def do_GET(self): | |
CONTENT = 'FROM: {0}:{1}\n\n'.format(self.client_address[0], self.client_address[1]) | |
CONTENT += "HEADERS: \n" | |
for h in self.headers: | |
CONTENT += '{0}: {1}\n'.format(h, self.headers[h]) | |
self.send_response(200) | |
self.send_header('Content-type', 'text/plain') | |
self.send_header('Content-length', len(str.encode(CONTENT))) | |
self.end_headers() | |
self.wfile.write(str.encode(CONTENT)) | |
def do_POST(self): | |
length = int(self.headers.get('content-length')) | |
message = self.rfile.read(length) | |
self.send_response(200) | |
self.send_header('Content-type', self.headers.get('content-type', 'text/plain')) | |
self.end_headers() | |
self.wfile.write(message) | |
class CustomTCPServer(TCPServer): | |
allow_reuse_address = True | |
# TODO: also listen on ipv6 https://docs.python.org/3/library/socket.html#socket.create_server | |
handler = CustomHandler | |
httpd = CustomTCPServer(('',PORT), handler) | |
print(f'Serving forever at port http://{socket.gethostname()}:{PORT}') | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment