How To: Simple HTTP Server with Python

Linux System Calls Start simple HTTP server with Python

When building new infrastructure elements and deploying servers, quite often you need to test firewall rules before the rest of application stack is deployed. The basic tool of my choice here is curl which is great to testing TCP connections. But it has an important dependency: you actually need to have something listening on the other end of the connection you’re testing. If there’s no software running and servicing the port you specify, you will receive an error.

Traditionally there have been small programs or scripts you’d write - first (many years ago now) in C, later in Perl. They would imply that you have to bring your test code or compiled binary to the server you need to test.

Today I’d like to share a super easy way to start a basic HTTP server with Python - it’s literally just one line that will work in most cases since Python is now ubiqutous enough to be installed by default in most Linux distributions.

How To Start HTTP server with Python

You need to use a Python module. It’s a different module for Python 2 and Python 3.

For Python 3, here’s how you start an HTTP server:

$ python3 -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...

In Python 2, you do the same by running a slightly different command:

$ python2 -m SimpleHTTPServer
Serving HTTP on port 8000 ...

Both commands can be stopped by the usual Ctrl+C combination.

How To Specify HTTP Server Port in Python

By just adding a port number to each of the command lines you can make Python run your basic HTTP server on a specific port instead of the default 8000:

$ python3 -m http.server 3333
Serving HTTP on :: port 3333 (http://[::]:3333/) ...
$ python2 -m SimpleHTTPServer 3333
Serving HTTP on port 3333 ...

How To Test Connectivity Using Curl and HTTP server

Now that you’re learned how to start HTTP server on a specific port, it’s possible to use curl command for testing connectivity to that server.

Here’s how it looks when nothing is running:

greys@mcfly $ curl -v telnet://
*   Trying
* Connection failed
* connect to port 3333 failed: Connection refused
* Failed to connect to port 3333: Connection refused
* Closing connection 0
curl: (7) Failed to connect to port 3333: Connection refused
greys@mcfly $ curl -v telnet://
*   Trying
* Connected to ( port 3333 (#0)

And here’s how it looks with HTTP server listening and reponding:

For sake of completeness, here’s how it looks if server or some other firewall between our system and the server is blocking connection using the most typical DROP technique (curl just hangs there until you stop it with Ctrl+C):

greys@mcfly $ curl -v telnet://
*   Trying

That’s all for today. Have fun!

See Also

Contact Me

Follow me on Facebook, Twitter or Telegram:
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
24 Mar 2024

Homelab: Mac Pro 2013 32GB

30 Sep 2023

macOS Sonoma 14.0

26 Sep 2023

Video: What Browser Do You Use?

Recent Tweets