51

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?

orlp
  • 112,504
  • 36
  • 218
  • 315
Ming K
  • 1,117
  • 1
  • 13
  • 20
  • 2
    Did you try Ctrl+F5 in your browser for refresh, instead of F5? – zenpoy Aug 30 '12 at 10:29
  • I made a modern Python 3 version of the highest voted answer: https://gist.github.com/opyate/6e5fcabc6f41474d248613c027373856 – opyate May 14 '21 at 14:48

5 Answers5

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

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
  • 2
    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"` – Jörn Zaefferer Oct 06 '15 at 15:24
  • 2
    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:https://stackoverflow.com/questions/14496694/whats-default-value-of-cache-control, https://stackoverflow.com/a/31852117/239657, unless explicitly told not to (which "Cache-Control" here does). – Beni Cherniavsky-Paskin Apr 07 '19 at 13:46
  • 1
    Modernised for Python 3 -> https://gist.github.com/opyate/6e5fcabc6f41474d248613c027373856 – opyate May 14 '21 at 14:49
  • Almost, but you really have got to call the super methods. – mirabilos Jan 01 '23 at 14:11
37

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)
phoenix
  • 7,988
  • 6
  • 39
  • 45
DBrown
  • 5,111
  • 2
  • 23
  • 24
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)

zenpoy
  • 19,490
  • 9
  • 60
  • 87
5

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

apparat
  • 1,930
  • 2
  • 21
  • 34
  • 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? – nirvanaswap Aug 17 '16 at 02:30
  • 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" – jbiWeisendorf Aug 20 '19 at 10:08
1

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
S.Doe_Dude
  • 151
  • 1
  • 5