Local Testing Server with Python30 Oct 2018
Table of Contents
This quick little tutorial shows how to setup a simple local testing server with Python.
Setting up a Testing Server
You can run run local files in the browser, but some times they won’t work or will give you some headache with things like Cross-Origin Resource Sharing (CORS). The main problems include:
- They feature asynchronous requests. Some browsers (including Chrome) will not run async requests (see Fetching data from the server) if you just run the example from a local file. This is because of security restrictions (for more on web security, read Website security).
- They feature a server-side language. Server-side languages (such as PHP or Python) require a special server to interpret the code and deliver the results. (Source)
You can start a server in Python 3 with:
python3 -m http.server
If you are still using Python 2 you can start the server with:
python -m SimpleHTTPServer
The server will run the contents of the directory on
localhost and port
8000. If you need another port, you can add the port at the end of the command (
python3 -m http.server 7800 or
python -m SimpleHTTPServer 7800 for Python 3 and 2 respectively).