In this article, we explain how can you set up a simple local web server in python to test your local HTML file.
At the beginning of learning web development, we make a simple website and to test we just open HTML files in the browser via drag and drop or double click. When you directly open local HTML file in Browser it starts with file:// and followed by the path of the local directory like (file:///F:/user/project/index.html) but in the server, Web address path starts with http:// or https:// like (https://www.google.com).
Problem with testing local files without server
Server-side Language: Python and PHP require a special server to read some script and code to deliver the end result.
Asynchronous requests: Because of security restrictions most of the browser like Chrome and Firefox will not run async requests or fetching data from the server when you run local file without host server.
Set up and running a simple local web server in Python
- Install Python on your machine. If using Linux OS or Mac OS X Python already install in your computer. If you are using Window OS then you can download Python in their website and install.
- Visit Python website www.python.org.
- In the download section click on latest python version like “3.xxx”.
- Choose the Windows x86 python installer and download in your computer.
- Run python installer in your computer.
- Most important make sure to check and enable “Add Python 3.xxx to PATH” option in the checkbox.
- Click Install, after installation has finish click close in installer window.
- After that open command prompt in window and terminal in Linux and Mac OS X and check python has to install successfully in your machine or not, enter the command in terminal or cmd window.
- In the response of this command cmd or terminal return python version number. This means python install successfully in your machine.
- Now using the cd command to navigate the project directory. For example your project folder in my document.
- To start up the server in that directory, enter the following command in terminal.
- Now python will set up a local web server by default on port 8000 in that directory. After that access your HTML file in the web browser type URL localhost: 8000. Your HTML file run or serve locally on localhost.
Note: If your Port 8000 use other application you can switch to the different port to run your python server to do this enter a command in terminal