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. |
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.
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' ... })