Created
June 13, 2017 20:30
-
-
Save m-x-k/d18dcb68abd22ed7b513540e88201890 to your computer and use it in GitHub Desktop.
Python flask after_this_request example on matching endpoint
This file contains hidden or 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 flask import Flask, g, request | |
| from flask import render_template, redirect | |
| app = Flask(__name__) | |
| def after_this_request(f): | |
| if not hasattr(g, 'after_request_callbacks'): | |
| g.after_request_callbacks = [] | |
| g.after_request_callbacks.append(f) | |
| return f | |
| @app.after_request | |
| def call_after_request_callbacks(response): | |
| for callback in getattr(g, 'after_request_callbacks', ()): | |
| callback(response) | |
| return response | |
| @app.before_request | |
| def before(): | |
| if request.path == "/match": | |
| @after_this_request | |
| def after(response): | |
| print("FOUND") | |
| @app.route('/') | |
| def empty(): | |
| return "EMPTY" | |
| @app.route('/match') | |
| def hello_test(): | |
| return "test" | |
| if __name__ == '__main__': | |
| app.run( | |
| debug=True, | |
| host="0.0.0.0", | |
| port=7100 | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment