Invoke Python SimpleHTTPServer from command line with no cache option

前端 未结 4 1171
臣服心动
臣服心动 2020-12-29 02:11

On Windows 7, I am using the command line

python -m SimpleHTTPServer 8888

to invoke a simple web server to serve files from a directory, f

4条回答
  •  暖寄归人
    2020-12-29 02:40

    Of course the script above will not work for Python 3.x, but it just consists of changing the SimpleHTTPServer to http.server as shown below:

    #!/usr/bin/env python
    import http.server
    
    class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
        def end_headers(self):
            self.send_my_headers()
            http.server.SimpleHTTPRequestHandler.end_headers(self)
    
        def send_my_headers(self):
            self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
            self.send_header("Pragma", "no-cache")
            self.send_header("Expires", "0")
    
    
    if __name__ == '__main__':
        http.server.test(HandlerClass=MyHTTPRequestHandler)
    

提交回复
热议问题