Skip to content

Instantly share code, notes, and snippets.

@phrawzty phrawzty/cli.md
Last active May 10, 2019

Embed
What would you like to do?
simple http server to dump request headers
$ 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 -
#!/usr/bin/env python
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()
@ghost

This comment has been minimized.

Copy link

commented Mar 23, 2017

Good work , helped for me

@avicoder

This comment has been minimized.

Copy link

commented Oct 13, 2017

Thanks

@apehovski

This comment has been minimized.

Copy link

commented Oct 20, 2017

Thanks a lot!

@pilgrim2go

This comment has been minimized.

Copy link

commented Dec 6, 2017

Thanks

@up_the_irons

This comment has been minimized.

Copy link

commented Jan 23, 2018

Bad ass! Solved my problem straight away.

@snaka

This comment has been minimized.

Copy link

commented Feb 15, 2018

👍

@sscarduzio

This comment has been minimized.

Copy link

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

commented Feb 27, 2018

Thanks! Exactly what I was looking for.

@vikas027

This comment has been minimized.

Copy link

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

commented Apr 17, 2018

❤️

@ernesto-alvarado

This comment has been minimized.

Copy link

commented Sep 21, 2018

cool, thanks

@thilinapiy

This comment has been minimized.

Copy link

commented Nov 19, 2018

👍

@vishes-shell

This comment has been minimized.

Copy link

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

commented Jan 9, 2019

Thanks!

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.