Connecting to the real world#

As shown in the Quickstart, Drakken provides a simple web server useful for developing your web app. Never use the built-in web server in production. Instead use an external web server.

What is WSGI?#

Your Drakken app receives a request for a web page and generates a response. That’s all. It doesn’t know how to communicate with clients such as web browsers; instead it uses an external web server to talk to them. The interface Drakken uses to talk to the web server is called WSGI.

WSGI is pretty simple: you need to provide a callable object (a function or instance with a call method) that expects two parameters:

  1. environ: a dictionary of environmental variables.

  2. start_response: a callback function for sending HTTP status and headers to the server.

That’s it. The drakken.core module provides Drakken’s WSGI interface.

WSGI web servers#

Two popular WSGI web servers are gunicorn and uWSGI. Gunicorn is easier to configure but runs on Unix only; uWSGI is faster. Both are good enough for most use cases so we’ll use gunicorn.

  1. Install gunicorn:

    pip3 install gunicorn

  2. Launch the demo app we created previously:

    gunicorn demo:app

  3. Visit the demo app’s home page. You should see a web page with the message Hello from the HOME page.

Gunicorn received the request for a web page and sent it to the Drakken demo app. The app generated a response and sent it back to the server which sent it to the client. Communication between server and app was handled through the WSGI protocol.