55

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, for development.

The problem is that the server seems to keep the files in cache. Old versions of files are served despite newer ones being available.

Is there a way to specify the "no cache" option from the command line directly?

3
  • 2
    Did you try Ctrl+F5 in your browser for refresh, instead of F5? Commented Aug 30, 2012 at 10:29
  • I made a modern Python 3 version of the highest voted answer: gist.github.com/opyate/6e5fcabc6f41474d248613c027373856 Commented May 14, 2021 at 14:48
  • What helped in my case (Firefox) was to open the developer console and tick "Disable cache" in the network tab. Ctrl+F5 did not help. Commented Feb 20, 2024 at 17:27

5 Answers 5

56

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

Sign up to request clarification or add additional context in comments.

5 Comments

Adding this to .bash_profile makes it more convenient to use: alias server="open http://localhost:8000/ && echo 'Starting server at localhost:8000' && python ~/serveit.py 8000 > /dev/null 2>&1"
A theory why this works: SimpleHTTPRequestHandler only sends Last-Modified header; it doesn't send max-age or any explicit info how long its valid but browsers cache by default for some heuristic time:stackoverflow.com/questions/14496694/…, stackoverflow.com/a/31852117/239657, unless explicitly told not to (which "Cache-Control" here does).
Almost, but you really have got to call the super methods.
This does not answer the question.
39

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)

2 Comments

Almost, but you really have got to call the super methods.
This does not answer the question.
11

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)

2 Comments

But but but, I'm using jQuery AJAX. Even setting ` $.ajaxSetup({ cache: false });` doesn't fix it.
Some people are debugging in Safari on their iPhone. No CTRL+F5 equivalent there.
6

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

2 Comments

I figured out this is the case for me, by trying load the page from Firefox instead of Chrome. However, how do I fix this on chrome?
This works for me in Firefox: Empty Browser Cache in Firefox (versions 4 to 67): Press the keys Ctrl + Shift + Del. A new window will open. Activate the option "Cache" and then click on "Delete now"
2

I changed to another port number and the updated file reflected on my browser.

ex.

python -m http.server -p 8000

python -m http.server -p 8001

1 Comment

:-D that's one way to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.