Simple HTTP Web Server and Client in Python
Creating simple python scripts to understand HTTP web server and client without framework
This time I’d like to show you how to make a simple HTTP server and client in python. It’s a bit different from other tutorials I’ve ever wrote and I’d like to say that I’m also a beginner in python. But, I’ll try to make sure you understand what I wrote because this tutorial is easy.
First, you need to know what HTTP is.
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web.
– HTTP definition based on Wikipedia
In simple words, HTTP is a protocol that used for world wide web communication. So, if you’re browsing the web, downloading data, and hosting a website, you’re using HTTP protocol.
There are many web server software that support HTTP protocol such as apache
, nginx
, and lighttpd
.
In this moment, I’d like to show you how to make something like that (a simple version obviously) in python language.
Step 1: Write HTTP server script using BaseHTTPServer module
Luckily, python provides us an HTTP server module, it’s called BaseHTTPServer
.
We use two classes from this module, namely BaseHTTPRequestHandler
and HTTPServer
.
We also need to use os
module to access file in your system.
First, we need to import those modules:
Next, write a custom class namely KodeFunHTTPRequestHandler
that implement BaseHTTPRequestHandler
.
In this tutorial, I just implement GET
command from HTTP.
To do that, we need to override do_GET()
function.
Our objective is to request an HTML file from server using our client (explained int the next step).
Script inside do_GET()
function is written to handle our objective.
Next we need to add our __main__
function.
We only need to create an HTTPServer
object with defined IP address and port.
We also need to pass HTTP request handler above as its parameter.
Finally, to make the server always run, we only need to call server_forever()
function.
Save it and name it whatever you want (e.g, kodefun-httpserver.py
).
The following snipped is the complete script of http server.
Step 2: Write a simple HTTP client
To check if our server is working fine, we need an HTTP client. You can use your web browser to do this. But, It’ll be cool if you make your own client.
To accomplish this, let’s make another script, let’s name it kodefun-httpclient.py
.
This script only have two responsibilites based on user entry.
It’ll download an HTML file by typing command GET file.html
.
This will loop forever until user decide to type exit
as a command.
The script is very simple to write. The following code snippet is the implementation of our simple HTTP client.
Step 3: Test using GET command
The final step, we’ll test is our scripts above work just fine. To run the server, type the following command in your command prompt or terminal:
If there is no problem, you’ll see "http server is running..."
. Next, we need to create a dummy HTML file, let’s call it dummy.html and copy the code below:
Save it to your root folder defined in your script. In this example I use c:/xampp/htdocs/
. Next, we need to run the client, type following command to run the client:
When the client is running type following code to get dummy.html
file:
If there is no problem, and it should be no problem, you’ll see the content of dummy.html
file.
That’s all I can do this time. See you on my next tutorial.
Downloads
Fork or download the completed project on GitHub.
Further Reading
If you’re interested in learning more about network programming in python, I suggest to read Mastering Python Networking by Eric Chou.
Cover Photo by Sai Kiran Anagani on Unsplash.