All Projects → ipinfo → Python

ipinfo / Python

Licence: apache-2.0
Official Python Library for IPinfo API (IP geolocation and other types of IP data)

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Python

local-ipv4-address
Get the LAN IPv4 address of the local machine based on the default gateway
Stars: ✭ 14 (-91.46%)
Mutual labels:  ip-address
Ip Database
🇨🇳免费IP地址数据库(纯真IP,QQwry,输出格式为国家、省、市、县、运营商),中文数据库,方便实用。
Stars: ✭ 736 (+348.78%)
Mutual labels:  ip-address
Ip2region
Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm
Stars: ✭ 9,836 (+5897.56%)
Mutual labels:  ip-address
ipcalc.el
IP calculator in Emacs Lisp
Stars: ✭ 23 (-85.98%)
Mutual labels:  ip-address
Ipwhois
Retrieve and parse whois data for IPv4 and IPv6 addresses
Stars: ✭ 432 (+163.41%)
Mutual labels:  ip-address
Ipnetwork
A library to work with CIDRs in rust
Stars: ✭ 64 (-60.98%)
Mutual labels:  ip-address
Hostess.swift
A Swift implementation of NSHost that works on iOS, OS X and tvOS. Hostess.swift is safe to use in a framework because it does not require a bridging header. Hostess is Swift 4.0 (or newer) only and replaces the Swift 2.x only Host.swift.
Stars: ✭ 27 (-83.54%)
Mutual labels:  ip-address
Fcipaddressgeocoder
iOS Geocoder for geocode device IP Address location using GeoIP service(s) and a block-based syntax. 💻🌍
Stars: ✭ 114 (-30.49%)
Mutual labels:  ip-address
Cuteit
IP obfuscator made to make a malicious ip a bit cuter
Stars: ✭ 475 (+189.63%)
Mutual labels:  ip-address
Cidram
CIDRAM: Classless Inter-Domain Routing Access Manager.
Stars: ✭ 86 (-47.56%)
Mutual labels:  ip-address
laravel
Official Laravel client library for IPinfo API (IP geolocation and other types of IP data)
Stars: ✭ 80 (-51.22%)
Mutual labels:  ip-address
Nsot
Network Source of Truth is an open source IPAM and network inventory database
Stars: ✭ 337 (+105.49%)
Mutual labels:  ip-address
Iplocation
Get ip location information.
Stars: ✭ 70 (-57.32%)
Mutual labels:  ip-address
django
Official Django Library for IPinfo API (IP geolocation and other types of IP data)
Stars: ✭ 36 (-78.05%)
Mutual labels:  ip-address
Phpipam
phpipam development repository
Stars: ✭ 1,578 (+862.2%)
Mutual labels:  ip-address
findip
🔧 Python Script For Finding All Available Local IP Addresses
Stars: ✭ 38 (-76.83%)
Mutual labels:  ip-address
Ipmonitor
IP Monitor is a simple application which monitors your public IP address for changes and lets you set different kinds of notifications such as email, audio, pop up or executing a command
Stars: ✭ 28 (-82.93%)
Mutual labels:  ip-address
Ip2location Go
Use IP2Location geolocation database to lookup the geolocation information with IP2Location Go Package. It can be used to determine country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather, MCC, MNC, mobile brand name, elevation and usage type that any IP address or hostname originates from.
Stars: ✭ 142 (-13.41%)
Mutual labels:  ip-address
Ip Num
A TypeScript/JavaScript library for working with ASN, IPv4, and IPv6 numbers. It provides representations of these internet protocol numbers with the ability to perform various IP related operations like parsing, validating etc. on them
Stars: ✭ 113 (-31.1%)
Mutual labels:  ip-address
Mmm Networkscanner
A module for MagicMirror which determines the status of devices on the network based on their MAC or IP address
Stars: ✭ 85 (-48.17%)
Mutual labels:  ip-address

IPinfo IPinfo Python Client Library

This is the official Python client library for the IPinfo.io IP address API, allowing you to lookup your own IP address, or get any of the following details for an IP:

  • IP geolocation / geoIP data (city, region, country, postal code, latitude and longitude)
  • ASN details (ISP or network operator, associated domain name, and type, such as business, hosting or company)
  • Firmographics data (the name and domain of the business that uses the IP address)
  • Carrier information (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)

Getting Started

You'll need an IPinfo API access token, which you can get by singing up for a free account at https://ipinfo.io/signup.

The free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as IP type and company data. To enable all the data fields and additional request volumes see https://ipinfo.io/pricing

Installation

pip install ipinfo

Quick Start

>>> import ipinfo
>>> access_token = '123456789abc'
>>> handler = ipinfo.getHandler(access_token)
>>> ip_address = '216.239.36.21'
>>> details = handler.getDetails(ip_address)
>>> details.city
'Mountain View'
>>> details.loc
'37.3861,-122.0840'

Async/Await

An asynchronous handler is available as well, and can be accessed and used in almost the same exact way as the synchronous handler:

>>> import ipinfo
>>> access_token = '123456789abc'
>>> handler = ipinfo.getHandlerAsync(access_token)
>>> ip_address = '216.239.36.21'
>>> async def do_req():
...     details = await handler.getDetails(ip_address)
...     print(details.city)
...     print(details.loc)
...
>>>
>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(do_req())
Mountain View
37.4056,-122.0775
>>>
>>> ip_address = '1.1.1.1'
>>> loop.run_until_complete(do_req())
New York City
40.7143,-74.0060

Internally the library uses aiohttp, but as long as you provide an event loop (as in this example via asyncio), it shouldn't matter.

Usage

The Handler.getDetails() method accepts an IP address as an optional, positional argument. If no IP address is specified, the API will return data for the IP address from which it receives the request.

>>> import ipinfo
>>> access_token = '123456789abc'
>>> handler = ipinfo.getHandler(access_token)
>>> details = handler.getDetails()
>>> details.city
'Mountain View'
>>> details.loc
'37.3861,-122.0840'

Authentication

The IPinfo library can be authenticated with your IPinfo API token, which is passed in as a positional argument. It also works without an authentication token, but in a more limited capacity.

>>> import ipinfo
>>> handler = ipinfo.getHandler(access_token='123456789abc')

Details Data

handler.getDetails() will return a Details object that contains all fields listed in the IPinfo developer docs with a few minor additions. Properties can be accessed directly.

>>> details.hostname
'any-in-2415.1e100.net'

Country Name

details.country_name will return the country name, as supplied by the countries.json file. See below for instructions on changing that file for use with non-English languages. details.country will still return country code.

>>> details.country
'US'
>>> details.country_name
'United States'

Longitude and Latitude

details.latitude and details.longitude will return latitude and longitude, respectively, as strings. details.loc will still return a composite string of both values.

>>> details.loc
'37.3861,-122.0840'
>>> details.latitude
'37.3861'
>>> details.longitude
'-122.0840'

Accessing all properties

details.all will return all details data as a dictionary.

>>> import pprint
>>> pprint.pprint(details.all)
{'abuse': {'address': 'US, CA, Mountain View, 1600 Amphitheatre Parkway, 94043',
           'country': 'US',
           'email': '[email protected]',
           'name': 'Abuse',
           'network': '216.239.32.0/19',
           'phone': '+1-650-253-0000'},
 'asn': {'asn': 'AS15169',
         'domain': 'google.com',
         'name': 'Google LLC',
         'route': '216.239.36.0/24',
         'type': 'business'},
 'city': 'Mountain View',
 'company': {'domain': 'google.com', 'name': 'Google LLC', 'type': 'business'},
 'country': 'US',
 'country_name': 'United States',
 'hosting': {'host': 'google',
             'id': 'GOOGLE',
             'name': 'Google LLC',
             'network': '216.239.32.0/19'},
 'hostname': 'any-in-2415.1e100.net',
 'ip': '216.239.36.21',
 'latitude': '37.3861',
 'loc': '37.3861,-122.0840',
 'longitude': '-122.0840',
 'postal': '94035',
 'region': 'California',
 'timezone': 'America/Los_Angeles'}

Caching

In-memory caching of details data is provided by default via the cachetools library. This uses an LRU (least recently used) cache with a TTL (time to live) by default. This means that values will be cached for the specified duration; if the cache's max size is reached, cache values will be invalidated as necessary, starting with the oldest cached value.

Modifying cache options

Cache behavior can be modified by setting the cache_options keyword argument. cache_options is a dictionary in which the keys are keyword arguments specified in the cachetools library. The nesting of keyword arguments is to prevent name collisions between this library and its dependencies.

  • Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency)
  • Default TTL: 24 hours (in seconds)
>>> import ipinfo
>>> handler = ipinfo.getHandler(cache_options={'ttl':30, 'maxsize': 128})

Using a different cache

It's possible to use a custom cache by creating a child class of the CacheInterface class and passing this into the handler object with the cache keyword argument. FYI this is known as the Strategy Pattern.

import ipinfo
from ipinfo.cache.interface import CacheInterface

class MyCustomCache(CacheInterface):
    ...

handler = ipinfo.getHandler(cache=MyCustomCache())

Modifying request options

Note: the asynchronous handler currently only accepts the timeout option, input the same way as shown below.

Request behavior can be modified by setting the request_options keyword argument. request_options is a dictionary in which the keys are keyword arguments specified in the requests library. The nesting of keyword arguments is to prevent name collisions between this library and its dependencies.

  • Default request timeout: 2 seconds
>>> handler = ipinfo.getHandler(request_options={'timeout': 4})

Internationalization

When looking up an IP address, the response object includes a details.country_name attribute which includes the country name based on American English. It is possible to return the country name in other languages by setting the countries_file keyword argument when creating the IPinfo object.

The file must be a .json file with the following structure:

{
  "BD": "Bangladesh",
  "BE": "Belgium",
  "BF": "Burkina Faso",
  "BG": "Bulgaria",
  ...
}

Batch Operations

Looking up a single IP at a time can be slow. It could be done concurrently from the client side, but IPinfo supports a batch endpoint to allow you to group together IPs and let us handle retrieving details for them in bulk for you.

>>> import ipinfo, pprint
>>> access_token = '123456789abc'
>>> handler = ipinfo.getHandler(access_token)
>>> pprint.pprint(handler.getBatchDetails([
...   '1.1.1.1',
...   '8.8.8.8',
...   '1.2.3.4/country',
... ]))
{'1.1.1.1': {'city': '',
             'country': 'AU',
             'country_name': 'Australia',
             'hostname': 'one.one.one.one',
             'ip': '1.1.1.1',
             'latitude': '-33.4940',
             'loc': '-33.4940,143.2100',
             'longitude': '143.2100',
             'org': 'AS13335 Cloudflare, Inc.',
             'region': ''},
 '1.2.3.4/country': 'US',
 '8.8.8.8': {'city': 'Mountain View',
             'country': 'US',
             'country_name': 'United States',
             'hostname': 'dns.google',
             'ip': '8.8.8.8',
             'latitude': '37.3860',
             'loc': '37.3860,-122.0838',
             'longitude': '-122.0838',
             'org': 'AS15169 Google LLC',
             'postal': '94035',
             'region': 'California',
             'timezone': 'America/Los_Angeles'}}

The input size is not limited, as the interface will chunk operations for you behind the scenes.

Please see the official documentation for more information and limitations.

Other Libraries

There are official IPinfo client libraries available for many languages including PHP, Go, Java, Ruby, and many popular frameworks such as Django, Rails and Laravel. There are also many third party libraries and integrations available for our API.

About IPinfo

Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, hosted domains, and IP type data sets. Our API handles over 20 billion requests a month for 100,000 businesses and developers.

image

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].