Skip to content

IPInfoManager

The IPInfoManager class handles IP geolocation using IPInfo's database.

Class Definition

class IPInfoManager:
    def __init__(self, token: str):
        """
        Initialize IPInfoManager with IPInfo token.
        """

Methods

initialize

async def initialize(self):
    """
    Initialize and download the database if needed.
    """

get_country

def get_country(self, ip: str) -> Optional[str]:
    """
    Get country code for an IP address.
    """

close

def close(self):
    """
    Close the database connection.
    """

Usage Example

from guard.handlers.ipinfo_handler import IPInfoManager

# Initialize database
ipinfo_db = IPInfoManager(token="your_token")
await ipinfo_db.initialize()

# Get country for IP
country = ipinfo_db.get_country("8.8.8.8")
print(f"Country: {country}")  # Output: "US"

# Clean up
ipinfo_db.close()