Created
July 23, 2014 17:34
-
-
Save daimatz/3ca32ae11d9635372853 to your computer and use it in GitHub Desktop.
SimpleHTTPServer with custom headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is for Python 2, which is EOL, so this should be updated for Python 3 which uses
http.server
instead.