-
-
Save meganehouser/31bd1fc717aa512a6ce23b5da9714efe to your computer and use it in GitHub Desktop.
SimpleHTTPServer with custom headers (Python3)
This file contains 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 python3 | |
import sys | |
from http.server import ( | |
SimpleHTTPRequestHandler, | |
HTTPServer, | |
test, | |
) | |
""" | |
Usage: | |
python3 httpd.py [port] [additional headers ...] | |
Example: | |
python3 httpd.py 8000 'Pragma: no-cache' 'Cache-Control: no-cache' 'Expires: 0' | |
""" | |
class CustomHTTPRequestHandler(SimpleHTTPRequestHandler): | |
def end_headers(self): | |
self.send_new_headers() | |
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__': | |
test(HandlerClass=CustomHTTPRequestHandler, ServerClass=HTTPServer, protocol="HTTP/1.1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment