look up the ISO country code (e.g. 'CN', 'US') for an ip address :param ip: ip address string :return: uppercase iso code, or None if unknown/unavailable
(ip)
| 13 | |
| 14 | |
| 15 | def get_country_iso(ip): |
| 16 | """ |
| 17 | look up the ISO country code (e.g. 'CN', 'US') for an ip address |
| 18 | :param ip: ip address string |
| 19 | :return: uppercase iso code, or None if unknown/unavailable |
| 20 | """ |
| 21 | if _reader is None: |
| 22 | return None |
| 23 | try: |
| 24 | record = _reader.get(ip) |
| 25 | except Exception: |
| 26 | return None |
| 27 | if not record: |
| 28 | return None |
| 29 | country = record.get('country') or record.get('registered_country') or {} |
| 30 | return country.get('iso_code') |
| 31 | |
| 32 | |
| 33 | if __name__ == '__main__': |
no outgoing calls
no test coverage detected