# Check line 1277 to line 1278 (main branch of Python currently): # https://github.com/python/cpython/blob/17b16e1/Lib/http/server.py#L1277-L1278 # # thus, `functools.partial` (closure/wrapper) will # make the parameter `protocol` of the function `test` useless. # # So, for anyone who specify a handler class directly, # everything is okay, its my bad to take the test function out of context. import os import socket import contextlib from functools import partial # [main branch of http/server.py]( # https://github.com/python/cpython/blob/17b16e1/Lib/http/server.py) # rename as the_main_branch_http to avoid native version conflict from the_main_branch_http import * import the_main_branch_http as http ############################################################################ # copy from http/server.py, begin ############################################################################ if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('--cgi', action='store_true', help='Run as CGI Server') 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]') args = parser.parse_args() if args.cgi: handler_class = CGIHTTPRequestHandler else: handler_class = partial(SimpleHTTPRequestHandler, directory=args.directory) # ensure dual-stack is not disabled; ref #38907 class DualStackServer(ThreadingHTTPServer): def server_bind(self): # suppress exception when protocol is IPv4 with contextlib.suppress(Exception): self.socket.setsockopt( socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) return super().server_bind() ############################################################################ # end (only the comment out part changed) ############################################################################ if "__main__" == __name__: # officially how the test function called # test( # HandlerClass=handler_class, # ServerClass=DualStackServer, # port=args.port, # bind=args.bind, # ) # mine http.test( HandlerClass=handler_class, port=args.port, bind=args.bind, protocol="any_but_not_the_BaseHTTPRequestHandler.protocol_version") # run this script by `python /path/to/my_http.py` # then, run a http client, e.g `curl -i http://127.0.0.1:8000` # # at last, check first line of the curl's output # it is not what parameter protocol specified (yes, its so long now), # but always "HTTP/1.0 200 OK"