Users can create a web server easily using Python in different ways. The blog explains these procedures to create a Python web server. We are considering Python because it offers easy ways to create a web server with unique functionalities.
How to Create a Python Web Server?
Explained below are different procedures to create a web server using Python:
First thing first, if your system doesn't have Python, then download it from the official website of Python.
Create an HTTP Server using Python
Open the terminal and locate the directory in which you want to create a root directory.
Now run the following command for starting a server:
Python 2 -- python -m SimpleHTTPServer 8000
Python 3 -- python -m http.server 8000
After that, open the web browser on http://localhost:8000/
The above commands will start the single-threaded HTTP server, which is bound to 0.0.0.0. We have used Port 8000 for creating servers, but you can choose any port according to the requirements.
Create a Builtin Web Server
First, execute the following command for starting a web server:
python3 -m http.server
The above command will open the webserver on port 8080 and then open the browser at http://127.0.0.1:8080/.
You can also access the webserver on the network by the 192.168.-.- address. Remember that it is a default server that is used for downloading files from a machine.
By running the command, you can start the custom web server:
# Python 3 server example
from http.server import BaseHTTPRequestHandler, HTTPServer
hostName = "localhost"
serverPort = 8080
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
In case you open the URL like http://127.0.0.1/example, the method do_GET() is called. A variable self.path returns a web browser URL requested.
That wraps our guide to create a Python web server using the simplest way possible. As discussed before, a web server and client speaks a standard language of the world wide web. Many people rely on the webserver for their important tasks, it is essential to have a proper server. We have provided complete details on the multiple procedures you can use to create a Python web server.