Run a simple lightweight Python server

When you are developing websites you often run into the need to test them on a server environment, because browsers block websites with certain features from running locally.

Here is a really quick way to run such a website in a server environment using Python 3 (which you probably have already installed on your machine anyways).

  1. Open the command window where your website root folder is. One easy way of doing that is simply typing “cmd” in the Windows Explorer address bar.
  2. Type this command in the command window:
python -m http.server 8000
  1. Now simply go to http://127.0.0.1:8000/ in your browser.

If you are getting errors regarding script files with that simple version, you can also try this longer code:

python
#Use to create local host
import http.server
import socketserver

PORT = 1337

Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
      ".js": "application/javascript",
});

httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()

And open that with:

http://127.0.0.1:1337/

Leave a Reply

Your email address will not be published. Required fields are marked *