How To Create a Simple HTTP Python Web Server (With Examples)

If we talk about web servers, it can be taken both in terms of hardware and software or even working together. No! There are more complexities behind the scenes that you are not aware of! There are various ways these are created and in this tutorial, you learn to create a Python 3\4 web server and use Flask to take it a step further and add custom configurations and scripts.

What are Web Servers Made of (In the core)

Underlying Hardware (Servers and Network): It is a system that can store web server software and site component files like HTML documents, pictures, CSS stylesheets, and JavaScript. It makes connections with the internet and provides support to physical data interchange with other devices connected to the web. They also include much larger processing power such as a dual CPU and 32-64 GB RAM.

It would be best to have a good high-speed internet connection (preferably fiber) hosted in a data center. This will make sure your bandwidth requirements are being met.

Software and OS: These are many parts that control how web users have access to hosted files. This is a minimum HTTP server, software that understands URLs and HTTP. You can access this via the domain names of the site it stored, and it delivers hosted website content to the end user’s device. Popular server operating systems include Ubuntu, CentOS, and Windows Server.

You can go more granular and look at their types too based on what type of content they host:

Static Web Server

It comes with a system with an HTTP server. This is called static because it sends all the hosted files as-is to the browser.

Dynamic Web Server

It carries a static web server and extra software, which is mostly an application server and database. Now, why is this called dynamic? It updates hosted files before sending content to the browser through an HTTP server.

The basics: At the basic level when the browser requires a file that is hosted on a web server, it requests it through HTTP. When the request reaches the correct web server it accepts it, then finds the document and sends it back to the browser via HTTP.

You can go further to look at server security and follow best practices for your code and server. Here, you will start coming into the territory of Firewalls and WAF.

READ ALSO: How To Find Hidden Admin Login Page of Any Website using Free tools.

How to Create Your Own Simple HTTP Web Server in Python (With Examples)

How a web server interacts with browser
How a web server interacts with the browser

Till now the best programming language is Python with apps in back-end development, machine learning, scientific modeling, system operations, and several enterprise-specific software. Why Python? It is approached easily with English-like syntax and many libraries.

This accessibility extends to creating a web Python server, which can be done in only a few lines of code.

Let’s see how to create a Python web server for local testing. The best thing about this is that it will not take much of your time hardly a few minutes with a few lines of code.

The entire process is quick yet straightforward and takes only a few minutes. This requires a line of code to get hands on the simple local servers running on the system.

By local testing, your system shall become the server to the client, the browser, whereas the files get stored on the system. Python HTTP server is the module that you will use to create a web server but there is one downside: you can only use this as a static web server; for dynamic, you need to have a Python framework such as Django.

Step 1: Run the below command to start the server:

Paul@ninja-ide:~# Python –m http.server

You need to type it in the terminal or command prompt depending on your system and you shall see a “server started” message and “server stopped” message when it gets closed.

This shall be your first Python web server. It is simple and can open up the server on your system’s default port of 80. The port however can be changed by specifying the port number at the end of the line, we have chosen port 1000. The default web server port is 80:

Step 2: Specify the port and it will start:

Paul@ninja-ide:~# Python –m http.server 1000

If we talk about a simple web server, it is good, whereas a custom web server is more interesting and educational. Do you want to learn Python in the best way? It is through a hands-on approach- code, debug, fix, rinse and repeat.

Step 3: Use custom code and framework to take your server to the next level.

You can also use built-in expressions and code in Python to build a custom web server, which, in turn, could be lighter.

Example use case:

You have just written a script of 100 lines or more, possibly 200, and want to test it quickly. Use the Pythons built-in option as shown above. It can handle and process basic to medium-sized scripts and HTML/CSS pages to be hosted locally.

Before putting ourselves in the critical part let’s keep in mind a few things, you shall see familiar terms in code if you have properly done your HTML homework. The class MyServer writes to the output stream (wfile) which sends a response to the client by using “self.wfile.write()”. Wondering what we are doing? We are simply writing an elementary HTML page on fly.

In the Python library, HTTP server is a standard module with classes used to communicate between the client and the server. The two classes are HTTPServer and BaseHTTPRequestHandler. The latter can access the server via the former. The other can store server addresses as instance variables, while BaseHTTPRequestHandler calls out methods to handle all the requests.

Create Python Web Server with Flask Framework

Build a Python Web Server with Flask Framework

Flash is a web framework that is primarily used in projects that require the development of small to large-scale web-based apps. It uses the Jinja2 template engine and it was developed by Armin Ronacher.

Following the steps below you can also create a web server using the Flash framework.

To ensure Flask is isolated, install it using (pip).

Step 1: Make sure you have Flask installed:

Paul@ninja-ide:~# install flask

Step 2: Make sure to import Flask so it is activated and running.

Paul@ninja-ide:~# python -c import flask

Example use case:

If you work on various small projects or build web-based apps then we recommend you build a web server with Flask. This is because these micro-apps don’t require dedicated resources and run just fine in an isolated environment that Flask can provide.

Educate yourself further on what can be done with Python:

Conclusion

Web servers power a large percentage of the web. For example, WordPress is used on 43% of all websites in the world. These blogs and sites require a web server either Apache or Nginx. This tutorial covered 2 different methods to create a simple HTTP web server with Python and Flask framework.

Previous articleRaspberry Pi Sh File: Install, Create and Run Shell/Bash Script (With Examples)
Next articleHow To Properly Set Up Your IDE Environment for Python Coding
Paul Carruthers
Paul is an avid programmer who specializes in Python and Java with over 16 years of experience in the field. He loves automating complex tasks and creating useful scripts to streamline work and make life easier. He is also a massive fan of Linux and currently uses it as his main desktop OS. When he is not staring at code, he loves hiking and swimming in different parts of the world.

LEAVE A REPLY

Please enter your comment!
Please enter your name here