Python Network Programming Routing
Python Web Programming: Routing
Routing is a mechanism for mapping URLs directly to the code used to create web pages. It helps better manage the structure of web pages, significantly increases website performance, and makes further enhancements or modifications very simple. In Python, routing is implemented in most web frameworks. In this chapter, we’ll look at an example using the Flask web framework.
Routing in Flask
The route() decorator in Flask is used to bind a URL to a function. Therefore, when the URL is referenced in a browser, the function is executed to produce the result. Here, the URL ‘/hello’ rule is bound to the hello_world() function. Therefore, if a user visits the http://localhost:5000/ URL, the output of the hello_world() function will be rendered in the browser.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello Tutorialspoint'
if __name__ == '__main__':
app.run()
When we run the above program, we get the following output −
* Serving Flask app "flask_route" (lazy loading)
* Environment: production
WARNING: Do not Use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [06/Aug/2018 08:48:45] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [06/Aug/2018 08:48:46] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [06/Aug/2018 08:48:46] "GET /favicon.ico HTTP/1.1" 404 -
We open a browser and point it to the URL http://localhost:5000/ See the results of the function being executed.
Using URL Variables
We can use routing to pass URL variables, allowing us to build URLs on the fly. To do this, we use the url_for() function, which accepts the function name as the first argument and the remaining arguments as the variable portion of the URL specification.
In the following example, we pass the function name as an argument to the url_for function and print out the results as the lines are executed.
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/')
def index(): pass
@app.route('/login')
def login(): pass
@app.route('/user/')
def profile(username): pass
with app.test_request_context():
print url_for('index')
print url_for('index', _external=True)
print url_for('login')
print url_for('login', next='/')
print url_for('profile', username='Tutorials Point')
When we run the above program, we get the following output −
/
http://localhost/
/login
/login?next=%2F
/user/Tutorials%20Point
Redirect
We can use the redirect function to redirect the user to another URL using routing. We specify the new URL as the function’s return value, which should be redirected to the user. This is useful when modifying an existing web page and temporarily transferring the user to a different page.
from flask import Flask, abort, redirect, url_for
app = Flask(__name__)
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/login')
def login():
abort(401)
# this_is_never_executed()
When the above code is executed, the base URL will be redirected to the login page, which uses the abort function so that the login page code will not be executed.