You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

103 lines
4.1 KiB

#!/usr/bin/env python
import os
import django
from pathlib import Path
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.develop')
django.setup()
from city_detection_ip import CITY_DB_PATH
import geoip2.database
def test_maxmind_capabilities():
"""Test what the MaxMind GeoLite2 database can actually do"""
if not CITY_DB_PATH.exists():
print(f"❌ Database file not found: {CITY_DB_PATH}")
return
print(f"✅ Found MaxMind database: {CITY_DB_PATH}")
try:
with geoip2.database.Reader(CITY_DB_PATH) as reader:
# Show what methods are available on the reader
print("\n📋 Available methods on MaxMind reader:")
methods = [method for method in dir(reader) if not method.startswith('_')]
for method in methods:
print(f" - {method}")
# Test IP lookup (this works)
print("\n🧪 Testing IP lookup (should work):")
try:
response = reader.city('8.8.8.8') # Google DNS IP
print(f" IP 8.8.8.8 → City: {response.city.name}, Country: {response.country.iso_code}")
except Exception as e:
print(f" ❌ IP lookup failed: {e}")
# Try to find coordinate-based methods (these won't exist)
print("\n🔍 Checking for coordinate-based methods:")
coord_methods = ['city_by_coords', 'location', 'reverse', 'geocode', 'coordinates', 'latlon']
found_coord_methods = [m for m in methods if any(coord in m.lower() for coord in coord_methods)]
if found_coord_methods:
print(f" ⚠️ Found potential coordinate methods: {found_coord_methods}")
for method in found_coord_methods:
try:
method_obj = getattr(reader, method)
print(f" - {method}: {method_obj}")
except Exception as e:
print(f" - {method}: Error - {e}")
else:
print(" ✅ No coordinate-based methods found (as expected)")
# Demonstrate the limitation
print("\n🚫 Demonstrating the limitation:")
print(" MaxMind databases only support IP → Location lookups")
print(" They do NOT support Coordinates → Location lookups")
print(" For reverse geocoding, you need a spatial database like our geonames_city table")
except Exception as e:
print(f"❌ Error opening database: {e}")
def test_coordinate_lookup_attempt():
"""Try to 'force' a coordinate lookup to show it doesn't work"""
print("\n🎯 Attempting coordinate lookup (this will fail):")
if not CITY_DB_PATH.exists():
return
try:
with geoip2.database.Reader(CITY_DB_PATH) as reader:
# Try various ways to pass coordinates (all will fail)
test_coords = [
"5.161.94.84", # Tehran coordinates
"35.698,51.4115", # String format
[35.698, 51.4115], # List format
]
for coords in test_coords:
try:
# This will raise AttributeError - no such method exists
response = reader.city(coords)
print(f" ❌ Unexpectedly succeeded with {coords}: {response}")
except AttributeError as e:
print(f" ✅ Expected error with {coords}: {e}")
except Exception as e:
print(f" ❓ Unexpected error with {coords}: {e}")
except Exception as e:
print(f"❌ Database error: {e}")
if __name__ == "__main__":
print("🧪 Testing MaxMind GeoLite2 Database Capabilities")
print("=" * 50)
test_maxmind_capabilities()
test_coordinate_lookup_attempt()
print("\n" + "=" * 50)
print("🎯 CONCLUSION:")
print("MaxMind GeoLite2 databases are for IP geolocation only.")
print("Use the geonames_city table for coordinate-based reverse geocoding.")