Skip to content

Instantly share code, notes, and snippets.

@phrawzty
Last active March 5, 2024 08:08
Star You must be signed in to star a gist
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 -
Copy link

ghost commented Mar 23, 2017

Good work , helped for me

@avicoder
Copy link

Thanks

@apehovski
Copy link

Thanks a lot!

@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! 🌟

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