l.s.g.helpers : module documentation

Part of lp.services.geoip

No module docstring
Function request_country Adapt a request to the country in which the request was made.
Function ipaddress_is_global Return True iff the IP address is on a global public network.
Function ipaddress_from_request Determine the IP address for this request.
def request_country(request):
Adapt a request to the country in which the request was made.

Return None if the remote IP address is unknown or its country is not in our database.

This information is not reliable and trivially spoofable - use it only for selecting sane defaults.

def ipaddress_is_global(addr):
Return True iff the IP address is on a global public network.
def ipaddress_from_request(request):

Determine the IP address for this request.

Returns None if the IP address cannot be determined or is not on a global public network.

The remote IP address is determined by the X-Forwarded-For: header, or failing that, the REMOTE_ADDR CGI environment variable.

Because this information is unreliable and trivially spoofable, we don't bother to do much error checking to ensure the IP address is at all valid, beyond what the ipaddress module gives us.

>>> ipaddress_from_request({'REMOTE_ADDR': '1.1.1.1'})
'1.1.1.1'
>>> ipaddress_from_request({
...     'HTTP_X_FORWARDED_FOR': '66.66.66.66',
...     'REMOTE_ADDR': '1.1.1.1'
...     })
'66.66.66.66'
>>> ipaddress_from_request({'HTTP_X_FORWARDED_FOR':
...     'localhost, 127.0.0.1, 255.255.255.255,1.1.1.1'
...     })
'1.1.1.1'
>>> ipaddress_from_request({
...     'HTTP_X_FORWARDED_FOR': 'nonsense',
...     'REMOTE_ADDR': '1.1.1.1'
...     })
API Documentation for Launchpad, generated by pydoctor at 2022-06-16 00:00:12.