HTTP and HTTPS server with Python

Python 2 has already included an HTTP server module in its later versions. And running the server is as simple as this:

It starts the server on port 8000 (by default), and if we want to run on a different port, we will just add the new port number at the end of the command.

With Python 3, the package merged into http.server. So we would call:

When we need to customize how the server behaves (e.g. to run the server on a specific IP address), we can write a very simple python codes:

If we want HTTPS server, we will need to use built-in “ssl” module. We need to wrap the httpd socket with wrap_socket method. The certificate needs to be in PEM format (RFC1422).

In general, we just need a socket and a handler to serve.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.