Skip to content

Instantly share code, notes, and snippets.

@daimatz
Created July 23, 2014 17:34
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save daimatz/3ca32ae11d9635372853 to your computer and use it in GitHub Desktop.
Save daimatz/3ca32ae11d9635372853 to your computer and use it in GitHub Desktop.
SimpleHTTPServer with custom headers
#!/usr/bin/env python
import SimpleHTTPServer
import BaseHTTPServer
import sys
"""
Usage:
python httpd.py [port] [additional headers ...]
Example:
python httpd.py 8000 'Pragma: no-cache' 'Cache-Control: no-cache' 'Expires: 0'
"""
class CustomHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_new_headers()
SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
def send_new_headers(self):
for i in sys.argv[2:]:
key, value = i.split(":", 1)
self.send_header(key, value)
if __name__ == '__main__':
BaseHTTPServer.test(HandlerClass=CustomHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer, protocol="HTTP/1.1")
@Tyler887
Copy link

This is for Python 2, which is EOL, so this should be updated for Python 3 which uses http.server instead.

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