Invoke Python SimpleHTTPServer from command line with no cache option

前端 未结 4 1167
臣服心动
臣服心动 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:51

    Perhaps this may work. Save the following to a file:

    serveit.py

    #!/usr/bin/env python
    import SimpleHTTPServer
    
    class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
        def end_headers(self):
            self.send_my_headers()
            SimpleHTTPServer.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__':
        SimpleHTTPServer.test(HandlerClass=MyHTTPRequestHandler)
    

    then run it using

    python serveit.py 8000
    

    to serve the current directory on port 8000. This was totally pulled from this gist, and seems to work great!

    NOTE: If you're just looking to run a local webserver to serve static content, you may be interested in a precanned node solution to do this => http-server, which I've been using and seems to work great.

    Also if you're on a mac, if you run this as root you can run it on port 80 or 443! For example

    sudo python serveit.py 80

    should allow you to run it and access it in your browser at http://localhost

提交回复
热议问题