-->

Python SimpleHTTPServer: change response header

2019-08-31 02:25发布

问题:

I use python's SimpleHTTPServer for tests applications.

Now for the test I need to change in the server response header field "Server". Now I have "Server: SimpleHTTP/0.6 Python/2.7.3" I would like something like "Server: Apache123".

Is it possible to change this field? Thx.

回答1:

If you wanted to have something easy to edit you could use this:

import SimpleHTTPServer
import BaseHTTPServer

def main():
    request_handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    request_handler.server_version = "Server: Apache123"
    request_handler.sys_version = ""
    BaseHTTPServer.test(HandlerClass = request_handler, ServerClass = BaseHTTPServer.HTTPServer)

if __name__ == "__main__":
    main()

You can run this the same way you run SimpleHTTPServer:

python you_script_name.py port

You could also edit it to take the name you want from the command line.