add GeolocationLookup.py

This commit is contained in:
ip2location 2024-09-20 09:16:28 +08:00
parent f0ea440d41
commit 625676e7ce
3 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,54 @@
import sys
import json
import argparse
import requests
# Define functions
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-k', '--apikey', metavar='IP2Location.io API Key.')
parser.add_argument('-p', '--ip', metavar='Specify an IP address.')
return parser
# Define variables
keyless = False
self_ip_query = False
parameters = {}
if len(sys.argv) > 1:
# check if API key is provided or not
parser = create_parser()
args = parser.parse_args(sys.argv[1:])
if args.apikey is None:
keyless = True
else:
parameters['key'] = args.apikey
if args.ip is None:
self_ip_query = True
else:
parameters['ip'] = args.ip
else:
# Assuming using Keyless API
keyless = True
if keyless:
print(f"Calling keyless API now...")
if len(parameters) > 0:
r = requests.get('https://api.ip2location.io/', params=parameters)
else:
r = requests.get('https://api.ip2location.io/')
else:
print(f"Calling API now...")
if len(parameters) > 0:
r = requests.get('https://api.ip2location.io/', params=parameters)
else:
r = requests.get('https://api.ip2location.io/')
if r.json() == None:
print(f"Unexpected error, please try again later.")
elif 'error' in r.json():
print(f"IP2Location.io API error: {r.json()['error']['error_message']}")
else:
print(r.json())

View File

@ -0,0 +1,43 @@
# Geolocation Lookup
This script make use of [IP2Location.io API](https://www.ip2location.io/) to query enrich geolocation information for an IP address. The script supports both normal API call and keyless API call. The keyless API had a limitation of 500 queries per day. Once this limit is reached, any additional API calls will fail until the time period resets at 00:00 UTC daily.
## Parameters
|Parameter| Description |
|--|--|
|-k, --apikey | IP2Location.io API Key. You can sign up a free one from https://www.ip2location.io/sign-up. If not provided, the script will use keyless API. |
|-p, --ip | IP address to be query for. If not provided, the source IP address will be use to query. |
## Usage
Before running the script, make sure install the required libraries first:
```
pip install -r requirements.txt
```
To perform normal query with the API Key, you can use `python GeolocationLookup.py -k <your_IP2Locatio.io_API_Key> -p <ip_address>`. Replaced the relevant values before run the command.
For keyless API call, you can run`python GeolocationLookup.py` or `python GeolocationLookup.py -p <ip_address>`.
For a successful call, you shall getting a JSON result printed out. For instance, you will see the following output for the IP address 8.8.8.8 for keyless API:
```json
{
"ip": "8.8.8.8",
"country_code": "US",
"country_name": "United States of America",
"region_name": "California",
"city_name": "Mountain View",
"latitude": 37.38605,
"longitude": -122.08385,
"zip_code": "94035",
"time_zone": "-07:00",
"asn": "15169",
"as": "Google LLC",
"is_proxy": false,
"message": "Limit to 500 queries per day. Sign up for a Free plan at https://www.ip2location.io to get 30K queries per month."
}
```

View File

@ -0,0 +1 @@
requests