{"library":"geoip2","code":"import os\nimport geoip2.database\nfrom geoip2.errors import AddressNotFoundError\n\n# --- Using a local GeoLite2 City database ---\n# 1. Download GeoLite2 City database from MaxMind (requires account):\n#    https://dev.maxmind.com/geoip/geolite2-free-geolocation-data\n# 2. Extract the .mmdb file (e.g., GeoLite2-City.mmdb) and place it in a known directory.\n\ndatabase_path = os.environ.get('GEOLITE2_CITY_DB_PATH', './GeoLite2-City.mmdb')\n\nif not os.path.exists(database_path):\n    print(f\"Error: GeoLite2 City database not found at {database_path}.\")\n    print(\"Please download it from MaxMind and update GEOLITE2_CITY_DB_PATH environment variable or file path.\")\nelse:\n    try:\n        # Reader objects are expensive to create and should be reused across lookups.\n        with geoip2.database.Reader(database_path) as reader:\n            ip_address = '8.8.8.8'\n            try:\n                response = reader.city(ip_address)\n                print(f\"IP: {ip_address}\")\n                print(f\"  Country: {response.country.name} ({response.country.iso_code})\")\n                print(f\"  City: {response.city.name}\")\n                print(f\"  Location: Latitude {response.location.latitude}, Longitude {response.location.longitude}\")\n            except AddressNotFoundError:\n                print(f\"IP address {ip_address} not found in the database.\")\n            except Exception as e:\n                print(f\"An error occurred during lookup for {ip_address}: {e}\")\n\n\n# --- Using the GeoIP2 Web Service (requires MaxMind Account ID and License Key) ---\n# MAXMIND_ACCOUNT_ID and MAXMIND_LICENSE_KEY should be set as environment variables\naccount_id = os.environ.get('MAXMIND_ACCOUNT_ID')\nlicense_key = os.environ.get('MAXMIND_LICENSE_KEY')\n\nif account_id and license_key:\n    print(\"\\n--- Web Service Lookup ---\")\n    try:\n        # Client objects are also expensive and should be reused.\n        with geoip2.webservice.Client(account_id, license_key) as client:\n            ip_address_ws = '1.1.1.1'\n            try:\n                response_ws = client.city(ip_address_ws)\n                print(f\"IP: {ip_address_ws}\")\n                print(f\"  Country: {response_ws.country.name} ({response_ws.country.iso_code})\")\n                print(f\"  City: {response_ws.city.name}\")\n                print(f\"  Location: Latitude {response_ws.location.latitude}, Longitude {response_ws.location.longitude}\")\n            except AddressNotFoundError:\n                print(f\"IP address {ip_address_ws} not found via web service.\")\n            except Exception as e:\n                print(f\"An error occurred during web service lookup for {ip_address_ws}: {e}\")\n    except Exception as e:\n        print(f\"Error initializing web service client: {e}\")\nelse:\n    print(\"\\nSkipping web service example: MAXMIND_ACCOUNT_ID and MAXMIND_LICENSE_KEY environment variables not set.\")","lang":"python","description":"This example demonstrates how to perform a geolocation lookup using a local GeoLite2 City database. It also includes an optional section for using the GeoIP2 Web Service, which requires a MaxMind account ID and license key. Remember to download a MaxMind database (e.g., GeoLite2-City.mmdb) and specify its path for the local database example. Reader and Client objects should be initialized once and reused for performance.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}