Flask get server host
How to get Flask server host address?
The flask python app host can be accessed (during a request) by running:
flask.request.host
# Or the full url:
flask.request.host_url
For more info read about app_context on the official Flask docs.
But wait, did you know you can also use url_for
?
url_for
allows you to pass _external=True
which will return a complete url with scheme (e.g. https://
) and the hostname (e.g. example.com
):
url_for('hello_world', _external=True)
Would return something like: https://example.com/
, which includes the server hostname.