Using Python HttpServer as a simple HTTP Server

Python Http Server

Hello everyone! In today’s article, we’ll take a look at using Python HttpServer.

This module serves as a very quick and easy way to start a local Http Server on your network.

Earlier, in Python 2.7, this module was called HttpServer. But with Python3, this module has been merged into the http.server module.

Let’s get started, and run our own Http Server!


Importing Python httpserver Module

This module comes as part of the standard library, so there’s no need to pip install it!

To import this module, simply use the below statement:

import http.server

Now you’re all set to run the server. Let’s now write a bit of code to serve the files.

Running our Http Server

If you simply want to share your files and directories to another user, you can directly run the server using Python.

Go to whatever directory you wish to share, and run the server from there, using:

python -m http.server 9000

Here, we start our local Http Server at port 9000.

Connecting to the Http Server

Now, to connect to the local server, you must do the following steps:

  1. Go to the server machine, and find out the server IP Address using arp -a on Windows or ip -a | grep inet on Linux.
  2. From the remote client, simply type http://IP_ADDRESS:9000/ on your browser and see the magic!

Output

Http Server Basic
Http Server Basic

Note that you can look at the server files, or even download it to the client machine!

Python Http Server File
Python Http Server File

Running a Python HttpServer that serves a custom index.html file

While the default server is a convenience for directly sharing files, you can customize the behavior of the server, by running a separate file.

For example, we’ll be running a custom Http Server which uses http.server and socketserver for TCP Communication.

The MyHttpRequestHandler calls do_GET() method to serve the request. To serve a custom file for the request, we can override the function by simply defining another do_GET() method that returns a different value.

# server.py
import http.server # Our http server handler for http requests
import socketserver # Establish the TCP Socket connections

PORT = 9000

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.path = 'index.html'
        return http.server.SimpleHTTPRequestHandler.do_GET(self)

Handler = MyHttpRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("Http Server Serving at port", PORT)
    httpd.serve_forever()

If you call this as server.py, you can run the http server using:

python server.py
Custom homepage Python http server
Custom Homepage for Python http server

Because we defined our custom do_GET() function, we can serve a homepage HTML file using our server which is index.html in this case. Also if the server is running on your system, you can directly access the server using localhost:<portnumber> instead of using the IP.


Conclusion

In this article, we learned how to set up a simple Http server in Python, using the http.server module. We also learned how to define the do_GET() method to serve custom files when we make a request to our server.

References