Python simple HTTP server for tests

Sometimes you need to kick off dummy HTTP server for purposes like tests. In this article we will have a look into this topic.

Python comes with http.server package. It's kinda nifty package but the documentation is typical Python-ish - API only, almost no examples and certainly no tutorial. However it's not that hard to set up simple HTTP server which can handle GET requests and even parse GET parameters.

Basically all you need to do is inherit BaseHTTPRequestHandler class. In your new class you create one method do_GET. All GET requests end up in this method. Sadly it doesn't take any parameteres which means you have to do all the work by yourself. I'm thinking of parsing parameters for example. Here is a simple example.

In this example we return all GET parameters we got in JSON format. First we set response code to 200, then content type. Method end_headers tells to server that all headers has been already set (send).

Next we parse URL path (the part after domain) and then parse we parse query string (the part after ?). When we are done and ready to send response back to client we basically write to self.wfile which represents output sink. Important thing is all the stuff we write must be in bytes - therefore we use bytes() function where we have to specify encoding the data comes from.

Our simple server is done now we can focus on how to start it without blocking our tests flow.

Running our HTTP server as non-blocking is kinda easy - threads. Yes we run the server in a separated thread because we need current thread to be free for running our tests.

What we did is we created a new function setup_server() which kicks off our HTTP server. Then we run this function in a new thread. Parameter daemon=True is important because it tells to Python to do-not-hang-up entire program when it already finished but still has some daemonic threads going on. In another words - daemonic thread will be killed once main thread (our program) exits. Lastly we sleep for like 1 second (can vary on slower computers). The reason is once we kick off the thread where our server is about to start we need to give it some time to actually initialize. If we would perform a request right after it's initialization without the sleep we could get an error.

That's it. I hope this dummy-server trick will help you.