Invoke Python SimpleHTTPServer from command line with no cache option

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

    Maybe it's the browser caching your files not the SimpleHTTPServer. Try deactivating the browser cache first.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-29 02:55

    I suggest that you press Ctrl+F5 when refreshing the browser.

    Just ran into this, it can just might be the thing you are looking for (it's in ruby, by the way)

    0 讨论(0)
提交回复
热议问题