Invoke Python SimpleHTTPServer from command line with no cache option

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)