banner



How To Create Server In Python

Build a Simple Web Server With Python

Create and run a simple server you can customize to your needs

Pavel Ilin

Photo by Johannes Groll on Unsplash.

Web servers are all around us. Every application, every framework uses a web server. It might sound fancy, but every web server does three things: It listens for HTTP requests, handles those requests, and sends responses back to the user. Let's try to build our own server with Python.

Nature of the HTTP Server

When you try to open Medium, your browser will create a network message in the form of an HTTP request. Requests will travel all the way through the internet to a computer where Medium has a running server that will handle this request by responding with HTML of the Medium home page. When your browser receives a response, it will render HTML so we can see the web page of the rendered error message in case something goes wrong.

Every interaction with the web page will send a new request and the server will respond with new HTML.

Let's see how the request can get to the server.

Importance of Listening

To get the destination, each HTTP message contains the destination TCP address. A TCP address is an IP address and port number.

Let's check where Medium hosts their website:

Now that we know the IP addresses, let's figure out the ports. By default, the port number is 80 for HTTP and 443 for HTTPS. And if the web server is listening on a non-conventional port, you have to specify where the HTTP message should go.

Web Server

Now that we more or less understand how requests travel over the internet and find their destinations, let's build a simple web server with Python.

First, we will need to import two libraries that will help us run the server and handle requests:

Next, we will declare the port at which the server will be listening for the requests and create a handler of the HTTP requests:

Finally, let's run the server on the port we declared:

Server HTML

Our server is ready to go, but we don't have anything to give the user back. Let's create a simple HTML document to render something in the browser:

Don't forget to place the HTML document and Python file in the same directory and name the HTML file index.html.

Run the Server

Now it's time to fire up the engines and run our server. Let's go to the directory where our Python file and HTML document are located. Run this command:

            python server.py          

Or this one (depends on which version of Python you use):

            python3 server.py          

And what we can see in our console? That server is serving at port 8000:

Now let's open a browser and navigate to the http://localhost:8000. Our HTML document has been served:

Server snippet

Conclusion

While our server is simple, it's able to serve HTML documents. Of course, the server that handles requests for Medium is much more complex, but the principle is the same. It listens for the requests and responds accordingly.

Keep learning, keep growing!

How To Create Server In Python

Source: https://betterprogramming.pub/web-server-with-python-cde3a9911a0f

Posted by: leveyoverects36.blogspot.com

0 Response to "How To Create Server In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel