|
| 1 | +# <img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/> IPinfo Python Client Library |
| 2 | + |
| 3 | +This is the official Python client library for the [IPinfo.io](https://ipinfo.io) IP address API, allowing you to lookup your own IP address, or get any of the following details for an IP: |
| 4 | + - IP geolocation (city, region, country, postal code, latitude and longitude) |
| 5 | + - ASN details (ISP or network operator, associated domain name, and type, such as business, hosting or company) |
| 6 | + - Company details (the name and domain of the business that uses the IP address) |
| 7 | + - Carrier details (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic) |
| 8 | + |
| 9 | + |
| 10 | +### Getting Started |
| 11 | + |
| 12 | +You'll need an IPinfo API access token, which you can get by singing up for a free account at [https://ipinfo.io/signup](https://ipinfo.io/signup?ref=lib-Python). |
| 13 | + |
| 14 | +The free plan is limited to 1,000 requests a day, 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](https://ipinfo.io/pricing?ref=lib-Python) |
| 15 | + |
| 16 | +#### Installation |
| 17 | + |
| 18 | +```pip install ipinfo``` |
| 19 | + |
| 20 | +#### Quick Start |
| 21 | + |
| 22 | +``` |
| 23 | +>>> import ipinfo |
| 24 | +>>> access_token = '123456789abc' |
| 25 | +>>> handler = ipinfo.getHandler(access_token) |
| 26 | +>>> ip_address = '216.239.36.21' |
| 27 | +>>> details = handler.getDetails(ip_address) |
| 28 | +>>> details.city |
| 29 | +Emeryville |
| 30 | +>>> details.loc |
| 31 | +37.8342,-122.2900 |
| 32 | +``` |
| 33 | + |
| 34 | +#### Usage |
| 35 | + |
| 36 | +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. |
| 37 | + |
| 38 | +``` |
| 39 | +>>> import ipinfo |
| 40 | +>>> access_token = '123456789abc' |
| 41 | +>>> handler = ipinfo.getHandler(access_token) |
| 42 | +>>> details = handler.getDetails() |
| 43 | +>>> details.city |
| 44 | +Emeryville |
| 45 | +>>> details.loc |
| 46 | +37.8342,-122.2900 |
| 47 | +``` |
| 48 | + |
| 49 | +#### Authentication |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +``` |
| 54 | +>>> access_token = '123456789abc' |
| 55 | +>>> handler = ipinfo.getHandler(access_token) |
| 56 | +``` |
| 57 | + |
| 58 | +#### Details Data |
| 59 | + |
| 60 | +`handler.getDetails()` will return a `Details` object that contains all fields listed in the [IPinfo developer docs](https://ipinfo.io/developers/responses#full-response) with a few minor additions. Properties can be accessed directly. |
| 61 | + |
| 62 | +``` |
| 63 | +>>> details.hostname |
| 64 | +cpe-104-175-221-247.socal.res.rr.com |
| 65 | +``` |
| 66 | + |
| 67 | +##### Country Name |
| 68 | + |
| 69 | + |
| 70 | +`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. |
| 71 | + |
| 72 | +``` |
| 73 | +>>> details.country |
| 74 | +US |
| 75 | +>>> details.country_name |
| 76 | +United States |
| 77 | +``` |
| 78 | + |
| 79 | +#### IP Address |
| 80 | + |
| 81 | + |
| 82 | +`details.ip_address` will return the an `ipaddress` object from the [Python Standard Library](https://docs.python.org/3/library/ipaddress.html). `details.ip` will still return a string. |
| 83 | + |
| 84 | +``` |
| 85 | +>>> details.ip |
| 86 | +104.175.221.247 |
| 87 | +>>> type(details.ip) |
| 88 | +<class 'str'> |
| 89 | +>>> details.ip_address |
| 90 | +104.175.221.247 |
| 91 | +>>> type(details.ip_address) |
| 92 | +<class 'ipaddress.IPv4Address'> |
| 93 | +``` |
| 94 | + |
| 95 | +##### Longitude and Latitude |
| 96 | + |
| 97 | + |
| 98 | +`details.latitude` and `details.longitude` will return latitude and longitude, respectively, as strings. `details.loc` will still return a composite string of both values. |
| 99 | + |
| 100 | +``` |
| 101 | +>>> details.loc |
| 102 | +34.0293,-118.3570 |
| 103 | +>>> details.latitude |
| 104 | +34.0293 |
| 105 | +>>> details.longitude |
| 106 | +-118.3570 |
| 107 | +``` |
| 108 | + |
| 109 | +##### Accessing all properties |
| 110 | + |
| 111 | +`details.all` will return all details data as a dictionary. |
| 112 | + |
| 113 | +``` |
| 114 | +>>> details.all |
| 115 | + { |
| 116 | + 'asn': { 'asn': 'AS20001', |
| 117 | + 'domain': 'twcable.com', |
| 118 | + 'name': 'Time Warner Cable Internet LLC', |
| 119 | + 'route': '104.172.0.0/14', |
| 120 | + 'type': 'isp'}, |
| 121 | + 'city': 'Los Angeles', |
| 122 | + 'company': { 'domain': 'twcable.com', |
| 123 | + 'name': 'Time Warner Cable Internet LLC', |
| 124 | + 'type': 'isp'}, |
| 125 | + 'country': 'US', |
| 126 | + 'country_name': 'United States', |
| 127 | + 'hostname': 'cpe-104-175-221-247.socal.res.rr.com', |
| 128 | + 'ip': '104.175.221.247', |
| 129 | + 'ip_address': IPv4Address('104.175.221.247'), |
| 130 | + 'loc': '34.0293,-118.3570', |
| 131 | + 'latitude': '34.0293', |
| 132 | + 'longitude': '-118.3570', |
| 133 | + 'phone': '323', |
| 134 | + 'postal': '90016', |
| 135 | + 'region': 'California' |
| 136 | + } |
| 137 | +``` |
| 138 | + |
| 139 | +#### Caching |
| 140 | + |
| 141 | +In-memory caching of `details` data is provided by default via the [cachetools](https://cachetools.readthedocs.io/en/latest/) 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. |
| 142 | + |
| 143 | +##### Modifying cache options |
| 144 | + |
| 145 | +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. |
| 146 | + |
| 147 | +* Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency) |
| 148 | +* Default TTL: 24 hours (in seconds) |
| 149 | + |
| 150 | +``` |
| 151 | +>>> handler = ipinfo.getHandler(cache_options={'ttl':30, 'maxsize': 128}) |
| 152 | +``` |
| 153 | + |
| 154 | +##### Using a different cache |
| 155 | + |
| 156 | +It's possible to use a custom cache by creating a child class of the [CacheInterface](https://github.com/ipinfo/python/blob/master/ipinfo_wrapper/cache/interface.py) class and passing this into the handler object with the `cache` keyword argument. FYI this is known as [the Strategy Pattern](https://sourcemaking.com/design_patterns/strategy). |
| 157 | + |
| 158 | +``` |
| 159 | +>>> handler = ipinfo.getHandler(cache=my_fancy_custom_class) |
| 160 | +``` |
| 161 | + |
| 162 | +#### Internationalization |
| 163 | + |
| 164 | +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. |
| 165 | + |
| 166 | +The file must be a `.json` file with the following structure: |
| 167 | + |
| 168 | +``` |
| 169 | + { |
| 170 | + "BD": "Bangladesh", |
| 171 | + "BE": "Belgium", |
| 172 | + "BF": "Burkina Faso", |
| 173 | + "BG": "Bulgaria" |
| 174 | + ... |
| 175 | + } |
| 176 | +``` |
| 177 | + |
| 178 | +### Other Libraries |
| 179 | + |
| 180 | +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. |
| 181 | + |
| 182 | +### About IPinfo |
| 183 | + |
| 184 | +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 and IP type data sets. Our API handles over 12 billion requests a month for 100,000 businesses and developers. |
| 185 | + |
| 186 | + |
0 commit comments