MCPcopy
hub / github.com/django/django / get_host

Method get_host

django/http/request.py:190–210  ·  view source on GitHub ↗

Return the HTTP host using the environment or request headers.

(self)

Source from the content-addressed store, hash-verified

188 return host
189
190 def get_host(self):
191 """Return the HTTP host using the environment or request headers."""
192 host = self._get_raw_host()
193
194 # Allow variants of localhost if ALLOWED_HOSTS is empty and DEBUG=True.
195 allowed_hosts = settings.ALLOWED_HOSTS
196 if settings.DEBUG and not allowed_hosts:
197 allowed_hosts = [".localhost", "127.0.0.1", "[::1]"]
198
199 domain, port = split_domain_port(host)
200 if domain and validate_host(domain, allowed_hosts):
201 return host
202 else:
203 msg = "Invalid HTTP_HOST header: %r." % host
204 if domain:
205 msg += " You may need to add %r to ALLOWED_HOSTS." % domain
206 else:
207 msg += (
208 " The domain name provided is not valid according to RFC 1034/1035."
209 )
210 raise DisallowedHost(msg)
211
212 def get_port(self):
213 """Return the port number for the request as a string."""

Calls 4

_get_raw_hostMethod · 0.95
DisallowedHostClass · 0.90
split_domain_portFunction · 0.85
validate_hostFunction · 0.85