Skip to content

Python, create a Web (HTTP) server

New Course Coming Soon:

Get Really Good at Git

Python makes it super easy to create an HTTP server, via the http module of the standard library.

In particular, the http.server object is the thing we’re going to use.

First, I want to mention one quick way to run an HTTP server from any folder, without writing any code:

python -m http.server --cgi 8000

This will run an HTTP server on port 8000, serving the files in the current folder. Of course it’s not an HTTP server fully featured like Nginx or Apache, but that’s often good enough for prototypes, or for your own test projects.

Let’s now use that module in our code to make it programmatically serve an “Hello, World!” string to anyone connecting on port 8000:

from http.server import BaseHTTPRequestHandler, HTTPServer

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World!"
        self.wfile.write(bytes(message, "utf8"))

with HTTPServer(('', 8000), handler) as server:
    server.serve_forever()

After running this program locally you can try connecting using a Web browser to port http://localhost:8000.

This serves the same Hello, World! string to anyone visiting the site on http://localhost:8000, on GET requests to any URL, complete with a 200 response and a Content-type: text/html header.

We write to wfile, which contains the output stream for writing a response back to the client.

It works on GET requests because we implemented the handler do_GET method.

You can also implement do_HEAD(), do_POST() and any other HTTP method:

from http.server import BaseHTTPRequestHandler, HTTPServer

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World! Here is a GET response"
        self.wfile.write(bytes(message, "utf8"))
    def do_POST(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World! Here is a POST response"
        self.wfile.write(bytes(message, "utf8"))

with HTTPServer(('', 8000), handler) as server:
    server.serve_forever()

Python has lots of different libraries we can use to handle applications built on top a Web server, including the very popular Flask and Django.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my Python Handbook
→ Get my Python Handbook

Here is how can I help you: