Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 -
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Mar 23, 2017

Good work , helped for me

@avicoder

This comment has been minimized.

Copy link

@avicoder avicoder commented Oct 13, 2017

Thanks

@apehovski

This comment has been minimized.

Copy link

@apehovski apehovski commented Oct 20, 2017

Thanks a lot!

@pilgrim2go

This comment has been minimized.

Copy link

@pilgrim2go pilgrim2go commented Dec 6, 2017

Thanks

@up_the_irons

This comment has been minimized.

Copy link

@up_the_irons up_the_irons commented Jan 23, 2018

Bad ass! Solved my problem straight away.

@snaka

This comment has been minimized.

Copy link

@snaka snaka commented Feb 15, 2018

👍

@sscarduzio

This comment has been minimized.

Copy link

@sscarduzio sscarduzio commented Feb 21, 2018

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

This comment has been minimized.

Copy link

@shivanshu21 shivanshu21 commented Feb 27, 2018

Thanks! Exactly what I was looking for.

@vikas027

This comment has been minimized.

Copy link

@vikas027 vikas027 commented Mar 24, 2018

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

This comment has been minimized.

Copy link

@looneym looneym commented Apr 17, 2018

❤️

@ernesto-alvarado

This comment has been minimized.

Copy link

@ernesto-alvarado ernesto-alvarado commented Sep 21, 2018

cool, thanks

@thilinapiy

This comment has been minimized.

Copy link

@thilinapiy thilinapiy commented Nov 19, 2018

👍

@vishes-shell

This comment has been minimized.

Copy link

@vishes-shell vishes-shell commented Nov 29, 2018

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

This comment has been minimized.

Copy link

@mclavel mclavel commented Jan 9, 2019

Thanks!

@maglub

This comment has been minimized.

Copy link

@maglub maglub commented Dec 1, 2019

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

@dgtal1

This comment has been minimized.

Copy link

@dgtal1 dgtal1 commented Jan 22, 2020

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

@rorysavage77

This comment has been minimized.

Copy link

@rorysavage77 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

This comment has been minimized.

Copy link

@josephmilla 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.