A web server responds to HTTP or Hypertext Transfer Protocol requests from a client and then sends back responses that contain status codes or contents like XML, HTML, JSON, etc. A web server and client speak a standardized language of WWW (World Wide Web).  This language is the reason why the Mozilla Netscape browser can still work well with the latest Nginx web server or Apache. 

Did you know that you can create a python web server easily using multiple ways? This blog explains these procedures on how to create a Python web server. We are considering Python because it offers easy ways to create a Python simple HTTP server with unique functionalities. 

How to Create a Python Web Server?

Explained below are different procedures to create a Python3 HTTP server. First things first, if your system doesn't have Python, then download it from the official website of Python

You can check out our article here to install Python on Windows, Linux or macOS

Now that you have Python installed on your system, we can go ahead with creating the server. 

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 web server 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
import time

hostName = "localhost"
serverPort = 8080

class MyServer(BaseHTTPRequestHandler):
  def do_GET(self):
      self.send_response(200)
      self.send_header("Content-type", "text/html")
      self.end_headers()
      self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
      self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
      self.wfile.write(bytes("<body>", "utf-8"))
      self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
      self.wfile.write(bytes("</body></html>", "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. 

Conclusion 

That wraps our guide to creating a Python web server using the simplest way possible. As discussed before, a web server and client speak 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. 

People also read: