When developing a website in Python, there is endless documentation on deploying a webserver, to the point where it can get a bit confusing. This codebase is to provide examples of each kind, mostly in order of basic to advanced, with the advanced ones being more suitable for production.
The main purpose of this was for me to run against a stress test with Gatling to check for and ensure there were no performance bottlenecks. Each application is as simple as possible, with a sleep for half a second to simulate fetching data from a database, or a remote API, before returning a small textual response.
- Ensure nothing else is running on port
8080. - Ensure you have Docker installed.
- Navigate into the folder of the example you are interested in.
- Run
bash build.shto build the Docker image. - Run
bash run.shto run the built image, using that example. - Navigate to localhost:8080 and you should see the webserver respond.
This is the most basic example and not recommended for production. It implements the webserver in python itself using http.server, which only implements basic security checks.
Implements a webserver through the use of uWSGI. By default uWSGI does preforking, so your app is loaded one time, and then forked.
This is the same as uWSGI, but with an nginx proxy in front. Unfortunately, this is still a work in progress and is not working yet.
Implements a webserver through gunicorn.
According to the gunicorn docs on how many workers,
one should NOT scale the number of workers to the number of clients you expect
to have. Gunicorn should only need 4-12 worker processes to handle hundreds or
thousands of requests per second. They recommend setting (2 x $num_cores) + 1
workers.
This example is like the previous example, but we use Nginx as a proxy. This is strongly recommended by the gunicorn deployment docs. This will buffer slow clients. Without this buffering Gunicorn will be easily susceptible to denial-of-service attacks. You can use Hey to check if your proxy is behaving properly.
If you want to add an example, or feel that one of the examples could be improved or sped up with some configuration changes, be sure to submit a merge request.