Last active
August 11, 2021 11:22
-
-
Save dejurin/ba63099800d0c202f9d12a913bd3c29d to your computer and use it in GitHub Desktop.
IP Geolocation API - Python requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://api.ipgeolocation.io/ipgeo?apiKey=API_KEY&ip=8.8.8.8 | |
# https://ipgeolocation.io/documentation/ip-geolocation-api.html | |
import requests | |
class IPGeoLocation: | |
url = 'https://api.ipgeolocation.io/ipgeo' | |
def __init__(self, apiKey): | |
self.apiKey = apiKey | |
def getInfo(self, ip: str = None): | |
try: | |
r = requests.get( | |
url=self.url, | |
params={ | |
'apiKey': self.apiKey, | |
'ip': ip, | |
} | |
) | |
except requests.exceptions.ConnectionError as err: | |
return False | |
except requests.exceptions.HTTPError as err: | |
return False | |
data = r.json() | |
if 'ip' in data: | |
return data | |
""""" | |
if __name__ == "__main__": | |
x = IPGeoLocation(apiKey='') | |
print(x.getInfo('8.8.8.8')) | |
""""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment