URL generation

In chapter9_4.py, we wanted to dynamically create a URL in the form of '/<hostname>/list_interfaces' in code:

from flask import Flask, url_for...@app.route('/<hostname>/list_interfaces')def device(hostname):    if hostname in routers:        return 'Listing interfaces for %s' % hostname    else:        return 'Invalid hostname'routers = ['r1', 'r2', 'r3']for router in routers:    with app.test_request_context():        print(url_for('device', hostname=router))...

Upon its execution, you will have a nice and logical URL, as follows:

(venv) $ python chapter9_4.py/r1/list_interfaces/r2/list_interfaces/r3/list_interfaces * Running on http://0.0.0.0:5000/ * Restarting with reloader

For now, you can think of app.text_request_context() as a dummy request object ...

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.