Get Host Name Without Port in Flask Get Host Name Without Port in Flask flask flask

Get Host Name Without Port in Flask


Building on Juan E's Answer, this was my

Solution for Python3:

from urllib.parse import urlparseo = urlparse(request.base_url)host = o.hostname


There is no Werkzeug (the WSGI toolkit Flask uses) method that returns the hostname alone.What you can do is use Python's urlparse module to get the hostname from the result Werkzeug gives you:

python 3

from urllib.parse import urlparseo = urlparse(request.base_url)print(o.hostname)

python 2

from urlparse import urlparse    o = urlparse("http://127.0.0.1:5000/")print(o.hostname)  # will display '127.0.0.1'


This is working for me in python-flask application.

from flask import Flask, requestprint "Base url without port",request.remote_addrprint "Base url with port",request.host_url