Amicheletti Python Flask
Amicheletti Python Flask
When routing some function to a URL, you can use function url_for() to Blueprints are objects similar to the Flask application object, but are not an
generate the URL to that function. actual application. They can record operations and endpoints routing and
Example, if you have something like deliver resources, and then they are registered to the application (can be
registered multiple times) under a specific URL.
@app.route('/user/<username>') def profile(user‐
Create a blueprint:
name): pass you use url_for('profile', username="Andr
e") to get the URL for that route. feed_blueprint = Blueprint('feed', __name__)
Use blueprint like an Flask app object:
That way you can avoid having to change the hardcoded URL everywhere in
the code. @feed_blueprint.route('\')
Register the blueprint to the real application
File Uploads app.register_blueprint(feed_blueprint, url_pref
ix='/feed')
To handle file uploads with Flask, the HTML form must be set with enctype="multipart/‐
Blueprint root folder
form-data"
feed_blueprint.root_path
Then you can use it from a dictionary in requests.files
To build url for Blueprints, put the name used in the object creation before the
Example:
function name:
f = request.files['the_file'] f.save('/var/www/uploads/uploa‐
url_for('feed.index')
ded_file.txt')
Also you can use the error handler just like the Flask object
@feed_blueprint.errorhandler(404)
Redirects and Errors
JWT (cont)
}
There are Reserved Claims (predefined), Public Claims (defined by users at IANA JSON Web Token
Registry) and Private Claims (custom claims agreed by both parties)
- Signature
To generate the signature, take the encoded header and payload, a secret and encode all that with the
algorithm used.
Example: HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(p
ayload), secret)
- Usage
Now when the user wants to access a protected route or resource, the user agent must send the JWT typically
in the Authorization header, using the Bearer schema, like this:
Authorization: Bearer <token>
Variable Rules
Add variable parts to a URL. You can also specify a converter to the
variable.
Request Object
Logging