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 0.0.0.0 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 0.0.0.0 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:

[email protected] $ curl -v telnet://192.168.1.66:3333
*   Trying 192.168.1.66...
* TCP_NODELAY set
* Connection failed
* connect to 192.168.1.66 port 3333 failed: Connection refused
* Failed to connect to 192.168.1.66 port 3333: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 192.168.1.66 port 3333: Connection refused
[email protected] $ curl -v telnet://192.168.1.66:3333
*   Trying 192.168.1.66...
* TCP_NODELAY set
* Connected to 192.168.1.66 (192.168.1.66) port 3333 (#0)
^C

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):

[email protected] $ curl -v telnet://192.168.1.66:3333
*   Trying 192.168.1.66...
* TCP_NODELAY set
^C

That’s all for today. Have fun!

See Also




Keep Learning with Me

Follow me on Facebook and Twitter or jump into Telegram chat!:
Recommended Software
I use Brave browser, it's awesome: Brave Browser I'm also a fan of SetApp for macOS: SetApp for macOS
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 Tweets