Skip to content

Instantly share code, notes, and snippets.

@phrawzty
Last active December 5, 2024 19:04
Show Gist options
  • Save phrawzty/62540f146ee5e74ea1ab to your computer and use it in GitHub Desktop.
Save phrawzty/62540f146ee5e74ea1ab to your computer and use it in GitHub Desktop.
simple python http server to dump request headers
#!/usr/bin/env python2
import SimpleHTTPServer
import SocketServer
import logging
PORT = 8000
class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()
#!/usr/bin/env python3
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging
PORT = 8000
class GetHandler(
SimpleHTTPServer.SimpleHTTPRequestHandler
):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()
$ curl -s -H "X-Something: yeah" localhost:8000 > /dev/null
$ python serv.py
ERROR:root:User-Agent: curl/7.37.1
Host: localhost:8000
Accept: */*
X-Something: yeah

127.0.0.1 - - [05/Mar/2015 11:28:33] "GET / HTTP/1.1" 200 -
@adel-s
Copy link

adel-s commented Jun 30, 2024

POST method support?

Simple add do_METHOD for requred methods:

#!/usr/bin/env python3
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging

PORT = 8080

class GetHandler(
        SimpleHTTPServer.SimpleHTTPRequestHandler
        ):

    def do_GET(self):
        logging.error(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        logging.error(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_WHATEVER(self):
        logging.error(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)

httpd.serve_forever()
curl -XPOST localhost:8080
curl -XWHATEVER localhost:8080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment