Skip to content

Instantly share code, notes, and snippets.

@meganehouser
Forked from daimatz/httpd.py
Created February 29, 2020 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meganehouser/31bd1fc717aa512a6ce23b5da9714efe to your computer and use it in GitHub Desktop.
Save meganehouser/31bd1fc717aa512a6ce23b5da9714efe to your computer and use it in GitHub Desktop.
SimpleHTTPServer with custom headers (Python3)
#!/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