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"]environis a dict describing the request, with keys such asREQUEST_METHOD,PATH_INFOandQUERY_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.
The explanation opens here once every check passes.