Python SimpleHTTPServer: change response header

断了今生、忘了曾经 提交于 2020-01-25 11:11:11

问题


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.



来源:https://stackoverflow.com/questions/22308786/python-simplehttpserver-change-response-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!