geolocalizar_ip_metodo1()
Primary IP geolocation method using the ip-api.com service. This method provides comprehensive geolocation data including ISP information and does not require an API key.Function Signature
Parameters
The IPv4 address to geolocate. Must be a valid IP address format (e.g., “8.8.8.8”). Private IP addresses will return an error from the API.
Return Value
Returns a dictionary with geolocation data on success, or
None if the request fails.Response Data Structure
The IP address that was queried
Full country name (e.g., “United States”)
Two-letter country code (e.g., “US”)
Region or state name
City name
Postal/ZIP code
Latitude coordinate
Longitude coordinate
Timezone identifier (e.g., “America/New_York”)
Internet Service Provider name
Organization name
Autonomous System information
Code Example
Side Effects
- Creates
Resultados_Tracker/directory if it doesn’t exist - Saves results to a text file:
IP_{ip_address}_{timestamp}.txt - Prints formatted output to console with colored text
- Includes Google Maps URL in output file
Exceptions and Error Handling
Raised when the API request exceeds 10 seconds. Function catches this and returns
None.Raised when unable to connect to the API server. Function catches this and returns
None.If the API returns a non-200 status code, the function prints an error and returns
None.If the API returns
status: 'fail' in the response (e.g., for invalid or private IPs), the function prints the error message and returns None.API Details
- Endpoint:
http://ip-api.com/json/{ip_address} - Method: GET
- Timeout: 10 seconds
- Rate Limit: Free tier allows 45 requests per minute
- Authentication: None required
geolocalizar_ip_metodo2()
Alternative IP geolocation method using the ipinfo.io service. Provides similar data with slightly different field names and structure.Function Signature
Parameters
The IPv4 address to geolocate. Must be a valid IP address format (e.g., “8.8.8.8”).
Return Value
Returns a dictionary with geolocation data on success, or
None if the request fails.Response Data Structure
The IP address that was queried
Hostname associated with the IP address
City name
Region or state name
Two-letter country code (e.g., “US”)
Postal/ZIP code
Comma-separated latitude and longitude (e.g., “37.751,-97.822”)
Organization name (typically includes AS number and ISP)
Timezone identifier (e.g., “America/Chicago”)
Code Example
Side Effects
- Creates
Resultados_Tracker/directory if it doesn’t exist - Saves results to a text file:
IP_{ip_address}_metodo2_{timestamp}.txt - Prints formatted output to console with colored text
- Includes Google Maps URL in output file
- Parses
locfield to separate latitude and longitude
Exceptions and Error Handling
Raised when the API request exceeds 10 seconds. Function catches this and returns
None.Raised when unable to connect to the API server. Function catches this and returns
None.If the API returns a non-200 status code, the function prints an error and returns
None.If the API returns an error object in the response, the function prints the error message and returns
None.API Details
- Endpoint:
https://ipinfo.io/{ip_address}/json - Method: GET
- Timeout: 10 seconds
- Rate Limit: Free tier allows 50,000 requests per month
- Authentication: None required (free tier)
Comparison: Method 1 vs Method 2
| Feature | Method 1 (ip-api.com) | Method 2 (ipinfo.io) |
|---|---|---|
| ISP Field | Separate isp field | Included in org field |
| Country | Full name + code | Code only |
| Hostname | Not provided | Provided |
| AS Info | Separate as field | Included in org field |
| Coordinates | Separate lat/lon | Combined loc string |
| Rate Limit | 45 req/min | 50k req/month |
Best Practices
- Always validate the IP address using
validar_ip()before calling geolocation methods - Handle None returns - Check if the return value is
Nonebefore accessing data - Use Method 1 for more detailed ISP information
- Use Method 2 for hostname resolution
- Implement retry logic for production use cases to handle transient network errors
- Check rate limits if making multiple requests