13

I am trying to understand the functionality of WSGI and need some help.

So far I know that it is kind of a middleware between servers and applications, used for interfacing different application frameworks (that reside in the server side) with the application, provided that the framework in question has a WSGI adapter. Continuing the theoretical part, I know that for server to communicate with the application, server calls a callable (that takes two arguments: environment variables and start_response function). Here start_response function is provided by the server (?) and used by the application with a response status and header followed by response body.

I understand little of what I wrote above, so here are newbie questions: 1) What is the general call flow ? Application will provide the server with a callable and then server would invoke the application using that callable and using env_vars and start_response function as arguments?

2) What confuses me the most is that the application is sending the request headers and then it sends the response body as well. What type of request is this ?

Please enlighten me as I am unable to get my head around this stuff.

Thanks!

user1126425
  • 2,385
  • 4
  • 18
  • 17
  • 1
    Since you don't say what you have read so far, ensure you read the actual PEP if you haven't. http://www.python.org/dev/peps/pep-3333/ – Graham Dumpleton Jul 08 '13 at 15:47

1 Answers1

8

The call flow is as follow:

  1. The server got a http connection,
  2. server parsed the http request line and headers, read the body,
  3. server populates the environ dict according to the request,
  4. server calls application callable with environ and start_response as arguments,
  5. application callable calls start_response with response status and response headers,
  6. application return response body to the server,
  7. server send the http response to the client.

For your second problem, the request/response is an interface defined by wsgi protocol (e.g. status = '200 OK', response_headers = [('Content-type', 'text/plain')]), not the same thing with http request/response.

You can browse the stand library module wsgiref as reference.

freestyler
  • 5,224
  • 2
  • 32
  • 39
  • so is it all in one single process? (application and web server) – Pynchia Sep 06 '15 at 20:59
  • The step 4 must in single process. But the server which accepts http connection can be another process, e.g. it can pass the request info to another process using fastcgi, scgi, etc protocol. – freestyler Sep 07 '15 at 04:09
  • Thank you, but it would help to clarify what processes (OS processes) are involved and up to which point. For example in the case of apache w/mod_wsgi as a daemon. Would each request be handled by the very same process? Where is python executed? – Pynchia Sep 07 '15 at 05:30