Self-coded RSS A blog on Python, WebOb, programming technique and miscellaneous projects by Sergey Schetinin.
Getting WSGI ResponsesThanks to the WSGI spec and WebOb we can think about Python web applications as functions that take HTTP request as an argument and return HTTP response. That’s what happens on the conceptual level, but at the code level WSGI makes it look quite different. Unfortunately the argument to the WSGI callable is not just a representation of HTTP request and the return value isn’t the response either. It’s a shame, but we can work around it. What for? Here are some of the use cases:
First thing you should know is that every
After getting the Response object, we can edit the headers, body, set This is great, but if you remember, we want to look at the apps as functions taking a request, so the wsgi_to_funcThe first one is to wrap the wsgi app in a decorator that changes its calling signature to
The problem with this is that now the wsgi app gets two interfaces, the WSGI one and the wrapped one. resp = app << reqThe other solution will feel magic at first, but if you understand what’s happening, you’ll see that it’s not magical or fragile at all. What we do is subclass
What happens is that we overload the left-shift operator (<<) of the request object, when the request is the second argument. That makes The main benefit of this is that app can indeed be any wsgi app and unlike the Anyway, even you decide not to use it, you’ve got to agree that this is a neat trick. |