Invoke Python SimpleHTTPServer from command line with no cache option
Solution 1:
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
Solution 2:
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 python3
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)
Solution 3:
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)
Solution 4:
Maybe it's the browser caching your files not the SimpleHTTPServer. Try deactivating the browser cache first.