Last active
August 29, 2015 14:00
-
-
Save flibustenet/9cf280591cdc6f684662 to your computer and use it in GitHub Desktop.
tween and callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyramid.config import Configurator | |
from pyramid.view import view_config | |
from pyramid.events import NewRequest, NewResponse, subscriber | |
@view_config(route_name='hello', renderer='string') | |
def hello(request): | |
print "View hello" | |
return 'Hello' | |
def response_callback(request, response): | |
print "response_callback" | |
def finish(request): | |
print "finished_callback" | |
@subscriber(NewResponse) | |
def new_response(event): | |
print "NewResponse" | |
@subscriber(NewRequest) | |
def new_request(event): | |
print "NewRequest" | |
event.request.add_response_callback(response_callback) | |
event.request.add_finished_callback(finish) | |
def tween_factory(handler, registry): | |
def tween(request): | |
print "tween_in" | |
res = handler(request) | |
print "tween_out" | |
return res | |
return tween | |
if __name__ == '__main__': | |
config = Configurator() | |
config.add_tween('.tween_factory') | |
config.add_route('hello','/') | |
config.scan() | |
app = config.make_wsgi_app() | |
from wsgiref.simple_server import make_server | |
make_server('0.0.0.0', 6543, app).serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tween_in
NewRequest
View hello
tween_out
response_callback
NewResponse
finished_callback