en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How to create a Python web server

How to create a Python web server

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. 

VPS Tutorials May 31, 21 by Nisal N 3 min Read
How to create a Python web server

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
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 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. 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment