PythiloLesson 3 of 5 · progress kept in this browser

WSGI

WSGI is the standard interface between Python web apps and servers. An app is one callable with this shape:

def app(environ, start_response):
    start_response(
        "200 OK",
        [("Content-Type", "text/plain")],
    )
    return [b"hello"]
  • environ is a dict describing the request, with keys such as REQUEST_METHOD, PATH_INFO and QUERY_STRING.
  • start_response(status, headers) sets the status line and a list of header tuples.
  • The return value is an iterable of bytes.

Flask and Django are both WSGI apps underneath, and a server such as gunicorn just calls this function once per request.

Since it is only a function, you can test it with no server and no network. Build an environ dict, pass a start_response that records its arguments, and join the returned bytes.

Your task

Write a WSGI function app. GET / returns 200 OK with body hello. GET /echo?msg=x returns 200 OK with the decoded value of msg as UTF-8 (empty if absent). Any other path returns 404 Not Found. A method other than GET returns 405 Method Not Allowed with an Allow: GET header. Every response has Content-Type: text/plain; charset=utf-8.

Next: Flask routes