Request Parameter: What is it and why is it passed in?
You may have noticed the request argument which gets passed into every route function. This variable is a Request object and serves a very important purpose of holding valuable request information. Request objects have an abundance of functionality in them, which would be tedious to convey in such a short tutorial. Some concepts, such as usage of Deferred and callbacks, are formally introduced in other posts.
Write to the Frontend
Content can slowly be added to the frontend using the Request.write() function. Please note that the parameter must be a bytes type, Python 2.7 users don't have to worry as strings are bytes, however in Python 3, a string is NOT a bytes object.
@app.route('/write') def gradualWrite(request): request.write(b'<h1>Header</h1>') request.write(b'<h2>Header</h2>') request.write(b'<h3>Header</h3>') request.write(b'<h4>Header</h4>') request.write(b'<h5>Header</h5>')
Getting Query String or Form Arguments
A common use case for any web application would be to handle form data or a query string. The following will simply display all the form data or query string that's passed along.
@app.route('/args') def arguments(request): return '{0}'.format(request.args)
Now lets test this route using GET with query strings and POST using form data.
curl -X GET curl -X GET localhost:9000/args?Eminem=Superman\&Hoozier=Church
curl -X POST -d Adele=Hello -d Adele="Fire to the Rain" localhost:9000/args
One thing that catches some by surprise the fact that argument values are in a list, even if there's only a single value.
Redirects
@app.route('/redirect') def redirect(request): request.redirect('https://www.yahoo.com')
Finished Request
Execute a function after the request has finished using Request.notifyFinish(). This uses Deferred and callbacks.
import time @app.route('/onfinish') def onfinish(request): def displayTime(null, starttime): now = time.time() print('end - start time = {0}'.format(now-start)) begintime = time.time() request.notifyFinish().addCallback(displayTime, begintime) return 'Request began at {0}'.format(begintime)
Cookies
@app.route('/cookies') def cookies(request): value = request.args.get(b'cookie', [b'default']) request.addCookie('cookie', value[0])
Sessions
Get a Session object:
@app.route('/show') def showSession(request): return b'Session id: ' + request.getSession().uid
Expire the session:
@app.route('/expire') def expireSession(request): request.getSession().expire() return 'Session expired'
Test:
curl -c _cookie.jar -b _cookie.jar localhost:9000/show
curl -c _cookie.jar -b _cookie.jar localhost:9000/expire
Sessions can be a bit complex. Read more about sessions at Twisted's site.
References
- interrupted.py - A more advanced example of how to use the Request object to execute a function after an interruption.
- Request API
- Session Basics
Comments
Post a Comment