Get Host Name Without Port in Flask

M Rijalul Kahfi picture M Rijalul Kahfi · Apr 6, 2014 · Viewed 19.5k times · Source

I've just managed to get my app server hostname in Flask using request.host and request.url_root, but both field return hostname with its port. I want to use field/method that returns only the hostname without having to do string replace, if any.

Answer

Paul Brackin picture Paul Brackin · Mar 30, 2019

Building on Juan E's Answer, this was my

Solution for Python3:

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