How to set up local web server in python?

python
 

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

 
Some scripts won’t run if you directly open files in the local directory. This is due verity of reason, but some are.

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

 
One of the easiest ways to set up and running a local web server in Python, so async requests have been solved.
 

Steps

  •  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.
 
  1. Visit Python website www.python.org.
  2. In the download section click on latest python version like “3.xxx”.
  3. Choose the Windows x86 python installer and download in your computer.
  4. Run python installer in your computer.
  5. Most important make sure to check and enable “Add Python 3.xxx to PATH” option in the checkbox.
  6. Click Install, after installation has finish click close in installer window.
 
python installer
  • 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.
                       Python –v

 

  • 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. 
 
 
                      cd c:UsersNitinDocumentswebsite
 
 
  •   To start up the server in that directory, enter the following command in terminal.
 
                           Python –m http.server
 
  •  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
Python –m http.server 7800.
After that, you can access your content in localhost: 7800.
 
 

I've loved ❤ technology and always curious about new tech innovation. I've been a technology writer and passionate blogger for last 3 year and write How to guide to help people become a tech-savvy.

Leave a Comment