Setting up a simple HTTP server using Python
Last Updated :
02 Sep, 2020
In this article, we are going to learn how to set up a simple and local HTTP server using Python. An HTTP server can be very useful for testing Android, PC or Web apps locally during development. It can also be used to share files between two devices connected over the same LAN or WLAN network.
Installation
On the terminal run the following statement:
python3 -m http.server
or
python -m http.server
Python3 server commandAccessing the server locally
For accessing the server locally you need to visit http://localhost:8000/. Here you can see all the directories of your local storage along with all the data. You can also access an HTML page, It will be rendered by your web browser as you access it.
The localhost pageAccessing the server over a Network
Before going into application make a note that the device on which you have run the above commands is called a Server or a Host and the second device that you will be using to access the Server over the network is called Client. For accessing the server over a Network make sure that both the device(Server and the Client) are connected over the same LAN or WLAN network. To access the Server you need the IP address of the server.
For obtaining the IP address the following steps are to be followed on your Server device:
On the Windows command prompt, execute the following statement:
ipconfig
On the Linux, Unix or macOS terminal, execute the following statement:
ifconfig
Note the IP address returned by the above command. We will use this IP address further.
The result of 'ifconfig' command in Linux.
Once you know the IP address open any web browser on the Client device and type in the IP address of the first machine(Server device), along with port 8000: http://[ip address]:8000
Congratulations!! Now you know how to host a simple HTTP server using Python.
Note: This HTTP server has limited security only so use it only for development purposes or sharing files locally it's not recommended for use over a production environment.
Similar Reads
Simple Port Scanner using Sockets in Python Prerequisites: Socket Programming in Python Before going to the programming, let us discuss about ports. In this article, we will check the virtual ports of a server or websites, or localhost. Every port has a unique number. There are 65,535 ports available in a host starting from 0. We can assign t
3 min read
Launch an HTTP Server with a Single Line of Python Code Python, with its built-in capabilities, offers a simple and effective way to do this. With just a single line of code, you can launch an HTTP server to serve files from your local directory. This feature can be incredibly handy for developers, educators, and anyone needing a quick and easy way to sh
2 min read
GET and POST Requests Using Python This post discusses two HTTP (Hypertext Transfer Protocol) request methods  GET and POST requests in Python and their implementation in Python. What is HTTP? HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a cli
7 min read
File Sharing App using Python Computer Networks is an important topic and to understand the concepts, practical application of the concepts is needed. In this particular article, we will see how to make a simple file-sharing app using Python. Â An HTTP Web Server is software that understands URLs (web address) and HTTP (the proto
4 min read
How To Send Files Using Python Built-In Http Server Python's built-in HTTP server offers a straightforward way to share files over a local network or the internet without the need for complex setups. In this tutorial, we'll walk through the step-by-step process of using Python's built-in HTTP server to send files to clients. Setting Up the HTTP Serve
2 min read
Test the given page is found or not on the server Using Python In this article, we are going to write a Python script to test the given page is found or not on the server. We will see various methods to do the same. Method 1: Using Urllib. Urllib is a package that allows you to access the webpage with the program. Installation: pip install urllib Approach: Impo
2 min read