Skip to content

Instantly share code, notes, and snippets.

@arulrajnet
Last active September 1, 2022 00:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arulrajnet/eeda1ff8f42f11eab5aa6cf5570edeaf to your computer and use it in GitHub Desktop.
Save arulrajnet/eeda1ff8f42f11eab5aa6cf5570edeaf to your computer and use it in GitHub Desktop.
HTTP 1.0 upstream device for nginx proxy testing.
#!/usr/bin/env python3
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler
import os
import sys
def log(*value, sep=' ', end='\n', file=sys.stdout, flush=False):
print(*value, sep=sep, end=end, file=file, flush=flush)
def run(args):
fmt_cls = ArgumentDefaultsHelpFormatter
desc = 'Simple HTTP Server with strictly HTTP/1.0 as request.'
parser = ArgumentParser(description=desc, formatter_class=fmt_cls)
parser.add_argument('--bind', '-b', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: all interfaces]')
parser.add_argument('--directory', '-d', default=os.getcwd(),
help='Specify alternative directory '
'[default:current directory]')
parser.add_argument('port', action='store',
default=8000, type=int,
nargs='?',
help='Specify alternate port [default: 8000]')
conf = parser.parse_args(args)
try:
host = conf.bind
port = conf.port
if host == '*' or len(host.strip()) == 0:
host = '0.0.0.0'
except Exception as e:
log('invalid host:port for bind')
log(e)
sys.exit(1)
else:
conf.host = host
conf.port = port
http_version = "HTTP/1.0"
server_address = (conf.host, conf.port)
handler_class = partial(SimpleHTTPRequestHandler,directory=conf.directory)
handler_class.request_version = http_version
handler_class.protocol_version = http_version
httpd = HTTPServer(server_address, handler_class)
log('Serving {} on http://{}:{}/'.format(http_version, conf.host if conf.host else '*', conf.port))
httpd.serve_forever()
if __name__ == "__main__":
try:
sys.exit(run(sys.argv[1:]))
except KeyboardInterrupt:
pass
@arulrajnet
Copy link
Author

arulrajnet commented Jan 9, 2022

How to use

python3 http10server.py -b localhost 8234

This is the same as python3 -m http.server -b localhost 8234

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