Package: aioipapi
aioipapiAsynchronous asyncio/aiohttp based client forhttps://ip-api.comIP geolocation API.ip-api.comis a fast, accurate, reliable API service for IP geolocation,
free for non-commercial use.aioipapipackage provides asynchronous API to use ip-api.com service in free and pro.
The package features:Support JSON APIendpointSupport Batch JSON APIendpointAccess topro servicewith API keyFree API rate limits controlCustomizable retrying when networking problemsYou must not use aioipapi package for commercial purposes without API key.InstallingUse pip for installing:pip install -U aioipapiUsageAll examples are provided for Python 3.7 and above.Uselocationcoroutine to locate your own IP:importasynciofromaioipapiimportlocationprint(asyncio.run(location())){'status': 'success', 'country': 'United States', 'countryCode': 'US', 'region': 'CA', 'regionName': 'California', 'city': 'Santa Clara', 'zip': '95051', 'lat': 37.3417, 'lon': -121.9753, 'timezone': 'America/Los_Angeles', 'isp': 'DigitalOcean, LLC', 'org': 'Digital Ocean', 'as': 'AS14061 DigitalOcean, LLC', 'query': 'XXX.XX.XXX.XXX'}Uselocationcoroutine to locate a domain name:print(asyncio.run(location('github.com'))){'status': 'success', 'country': 'Netherlands', 'countryCode': 'NL', 'region': 'NH', 'regionName': 'North Holland', 'city': 'Amsterdam', 'zip': '1012', 'lat': 52.3667, 'lon': 4.89454, 'timezone': 'Europe/Amsterdam', 'isp': 'GitHub, Inc.', 'org': 'GitHub, Inc.', 'as': 'AS36459 GitHub, Inc.', 'query': '140.82.118.3'}A domain location is supported only in JSON endpoint. Currently, batch JSON endpoint does not support domain names as query.
In other words, you cannot locate a list of domain names per time.Uselocationcoroutine to locate an IP with cusomized result fields and language:print(asyncio.run(location('8.8.8.8',fields=['continent','region','country'],lang='de'))){'status': 'success', 'continent': 'Nordamerika', 'country': 'Vereinigte Staaten', 'region': 'VA', 'query': '8.8.8.8'}Uselocationcoroutine to locate a list of IPs:print(asyncio.run(location(['1.0.0.1','1.1.1.1','8.8.4.4','8.8.8.8'],fields=['lat','lon','org'])))[
  {'status': 'success', 'lat': -27.4766, 'lon': 153.0166, 'org': 'APNIC and Cloudflare DNS Resolver project', 'query': '1.0.0.1'}, 
  {'status': 'success', 'lat': -27.4766, 'lon': 153.0166, 'org': 'APNIC and Cloudflare DNS Resolver project', 'query': '1.1.1.1'}, 
  {'status': 'success', 'lat': 39.03, 'lon': -77.5, 'org': 'Google Public DNS', 'query': '8.8.4.4'}, 
  {'status': 'success', 'lat': 39.03, 'lon': -77.5, 'org': 'Google Public DNS', 'query': '8.8.8.8'}
]You can customize the result fields and lang for each IP in the query list:ips=['77.88.55.66',{'query':'1.1.1.1','fields':['lat','lon','country'],'lang':'de'},{'query':'8.8.8.8','fields':['continent','country'],'lang':'ru'},]print(asyncio.run(location(ips,fields=['region','isp','org'])))[
  {'status': 'success', 'region': 'MOW', 'isp': 'Yandex LLC', 'org': 'Yandex enterprise network', 'query': '77.88.55.66'},
  {'status': 'success', 'country': 'Australien', 'lat': -27.4766, 'lon': 153.0166, 'query': '1.1.1.1'}, 
  {'status': 'success', 'continent': 'Северная Америка', 'country': 'США', 'query': '8.8.8.8'}
]In these cases the package uses Batch JSON API endpoint.Uselocation_streamasync generator to locate IPs from an iterable or async iterable:importasynciofromaioipapiimportlocation_streamasyncdeflocate():asyncforresinlocation_stream(['1.0.0.1','1.1.1.1','8.8.4.4','8.8.8.8']):print(res)asyncio.run(locate())location_streamalso supportsfieldsandlangoptions.location_streamalways uses Batch JSON API endpoint.UseIpApiClientclass:importasynciofromaioipapiimportIpApiClientasyncdeflocate():asyncwithIpApiClient()asclient:print(awaitclient.location())asyncio.run(locate())IpApiClientprovideslocationandlocation_streammethods similar to the corresponding non-member coroutines.UseIpApiClientclass with existingaiohttp.ClientSessioninstead of client own session:importasyncioimportaiohttpfromaioipapiimportIpApiClientasyncdeflocate():asyncwithaiohttp.ClientSession()assession:asyncwithIpApiClient(session=session)asclient:print(awaitclient.location())asyncio.run(locate())Usage of existing session also supported inlocationandlocation_streamnon-member coroutines.If you want to use unlimited pro ip-api service you can use your API key inlocation,location_streamfunctions andIpApiClient:asyncwithIpApiClient(key='your-api-key')asclient:...When API key is set, the package always uses HTTPS for connection withpro.ip-api.com.Free API Rate Limit Controlip-api service has rate limits in free API (without key).
Currently, there are 45 requests per minute for JSON endpoint and 15 requests per minute for Batch JSON endpoint.The package controls the rate limits usingX-RlandX-Ttlresponse headers.
In other words, you are unlikely to get 429 HTTP error when using free API.
When API key is being used, the rate limits are not being checked, because pro API is theoretically unlimited.Let's locate a lot of IPs for example:importasyncioimportsysimportlogginglogging.basicConfig(format='%(relativeCreated)d[%(levelname)s]%(message)s',level=logging.DEBUG,stream=sys.stderr,)fromaioipapiimportlocationasyncio.run(location(['8.8.8.8']*2000))798 [DEBUG] BATCH API rate limit: rl=14, ttl=60
900 [DEBUG] BATCH API rate limit: rl=13, ttl=59
1001 [DEBUG] BATCH API rate limit: rl=12, ttl=59
1103 [DEBUG] BATCH API rate limit: rl=11, ttl=59
1247 [DEBUG] BATCH API rate limit: rl=10, ttl=59
1391 [DEBUG] BATCH API rate limit: rl=9, ttl=59
1493 [DEBUG] BATCH API rate limit: rl=8, ttl=59
1595 [DEBUG] BATCH API rate limit: rl=7, ttl=59
1698 [DEBUG] BATCH API rate limit: rl=6, ttl=59
1809 [DEBUG] BATCH API rate limit: rl=5, ttl=58
1910 [DEBUG] BATCH API rate limit: rl=4, ttl=58
2015 [DEBUG] BATCH API rate limit: rl=3, ttl=58
2116 [DEBUG] BATCH API rate limit: rl=2, ttl=58
2216 [DEBUG] BATCH API rate limit: rl=1, ttl=58
2315 [DEBUG] BATCH API rate limit: rl=0, ttl=58
2367 [WARNING] API rate limit is reached. Waiting for 61 seconds by rate limit...
63464 [DEBUG] BATCH API rate limit: rl=14, ttl=60
63605 [DEBUG] BATCH API rate limit: rl=13, ttl=59
63695 [DEBUG] BATCH API rate limit: rl=12, ttl=59
63790 [DEBUG] BATCH API rate limit: rl=11, ttl=59
63894 [DEBUG] BATCH API rate limit: rl=10, ttl=59Retrying ConnectionThe client try to reconnect to the service when networking problems.
By default 3 attempts and 1 second between attempts are used. You can change these parameters byretry_attemptsandretry_delayparameters:fromaioipapiimportlocation,location_stream,IpApiClient...result=location('8.8.8.8',retry_attempts=2,retry_delay=1.5)stream=location_stream(['8.8.8.8','1.1.1.1'],retry_attempts=2,retry_delay=1.5)...asyncwithIpApiClient(retry_attempts=2,retry_delay=1.5):...Also you can change these parameters in the global config:fromaioipapiimportconfig,IpApiClientconfig.retry_attempts=2config.retry_delay=1.5...asyncwithIpApiClient():...LicenseMIT