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 -
@pilgrim2go
Copy link

Thanks

@up_the_irons
Copy link

Bad ass! Solved my problem straight away.

@snaka
Copy link

snaka commented Feb 15, 2018

👍

@sscarduzio
Copy link

Variant that echoes back the request headers to the sender as response headers

#!/usr/bin/env python

import SimpleHTTPServer
import SocketServer

PORT = 8000


class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        self.send_head()
        for h in self.headers:
            self.send_header(h, self.headers[h])
        self.end_headers()
        self.send_response(200, "")


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

httpd.serve_forever()

@shivanshu21
Copy link

Thanks! Exactly what I was looking for.

@vikas027
Copy link

I have packaged @sscarduzio 's script into a docker container.

Also, here are some examples to use the script (as a reverse proxy) with Nginx, Traefik, and HAProxy

@looneym
Copy link

looneym commented Apr 17, 2018

❤️

@ernesto-alvarado
Copy link

cool, thanks

@thilinapiy
Copy link

👍

@vishes-shell
Copy link

Python 3.7 version:

from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
import logging

PORT = 8000

class GetHandler(SimpleHTTPRequestHandler):

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


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

httpd.serve_forever()

@mclavel
Copy link

mclavel commented Jan 9, 2019

Thanks!

@maglub
Copy link

maglub commented Dec 1, 2019

Plus one, helped me plenty! Added def do_POST to solve my stuff.

@dgtal1
Copy link

dgtal1 commented Jan 22, 2020

What a nice piece of super helpful code! Thanks mate, it helped me a lot. Absolutely love it.

@rorysavage77
Copy link

rorysavage77 commented Mar 24, 2020

Revised for python3

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

PORT = 80

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()

@josephmilla
Copy link

josephmilla commented Jul 3, 2020

Python3 variant that echoes back the request headers to the sender as response headers and body

serve.py

#!/usr/bin/env python3

from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
import logging
import sys

try:
    PORT = int(sys.argv[1])
except:
    PORT = 8000

class GetHandler(SimpleHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200, self.headers)
        for h in self.headers:
            self.send_header(h, self.headers[h])
        self.end_headers()


Handler = GetHandler
httpd = TCPServer(('', PORT), Handler)

httpd.serve_forever()

Usage:

$ python3 serve.py

OR

$ python3 serve.py <PORT>

Sample response from curl:

$ curl -v localhost:8000 -H "Host: blah.com"
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: blah.com
> User-Agent: curl/7.71.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 Host: blah.com
< User-Agent: curl/7.71.1
< Accept: */*
<

Server: SimpleHTTP/0.6 Python/3.7.7
Date: Fri, 03 Jul 2020 01:52:09 GMT
Host: blah.com
User-Agent: curl/7.71.1
Accept: */*

* Closing connection 0

@gbts
Copy link

gbts commented Aug 24, 2023

and here's a python3 + HTTPS version:

#!/usr/bin/env python3

import ssl
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging

PORT = 443

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.socket = ssl.wrap_socket(httpd.socket, certfile="cert.pem", keyfile="key.pem", server_side=True)

httpd.serve_forever()

@joshdemir
Copy link

Great stuff. Thanks! 🌟

@subbu05
Copy link

subbu05 commented Apr 7, 2024

POST method support?

@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