Skip to main content

PyPI version GitHub Workflow Status

Official Python client library and CLI for the ipdata API

This is a Python client and command line interface (CLI) for the ipdata.co IP Geolocation API. ipdata offers a fast, highly-available API to enrich IP Addresses with Location, Company, Threat Intelligence and numerous other data attributes.

Note that you need an API Key to use this package. You can get a free one with a 1,500 daily request limit by Signing up here.

Visit our Documentation for more examples and tutorials.

asciicast

Installation

Install the latest version of the cli with pip.

pip install ipdata

or easy_install

easy_install ipdata

Library Usage

You need a valid API key from ipdata to use the library. You can get a free key by Signing up here.

Replace test with your API Key in the following examples.

Looking up the calling IP Address

You can look up the calling IP address, that is, the IP address of the computer you are running this on by not passing an IP address to the lookup method.

from ipdata import ipdata
from pprint import pprint
# Create an instance of an ipdata object. Replace `test` with your API Key
ipdata = ipdata.IPData('test')
response = ipdata.lookup()
pprint(response)

Looking up any IP Address

You can look up any valid IPv4 or IPv6 address by passing it to the lookup method.

from ipdata import ipdata
from pprint import pprint
# Create an instance of an ipdata object. Replace `test` with your API Key
ipdata = ipdata.IPData('test')
response = ipdata.lookup('69.78.70.144')
pprint(response)
Sample Response
{'asn': {'asn': 'AS6167',
         'domain': 'verizonwireless.com',
         'name': 'Cellco Partnership DBA Verizon Wireless',
         'route': '69.78.0.0/16',
         'type': 'business'},
 'calling_code': '1',
 'carrier': {'mcc': '310', 'mnc': '004', 'name': 'Verizon'},
 'city': None,
 'continent_code': 'NA',
 'continent_name': 'North America',
 'count': '1527',
 'country_code': 'US',
 'country_name': 'United States',
 'currency': {'code': 'USD',
              'name': 'US Dollar',
              'native': '$',
              'plural': 'US dollars',
              'symbol': '$'},
 'emoji_flag': '🇺🇸',
 'emoji_unicode': 'U+1F1FA U+1F1F8',
 'flag': 'https://ipdata.co/flags/us.png',
 'ip': '69.78.70.144',
 'is_eu': False,
 'languages': [{'name': 'English', 'native': 'English'}],
 'latitude': 37.751,
 'longitude': -97.822,
 'postal': None,
 'region': None,
 'region_code': None,
 'status': 200,
 'threat': {'is_anonymous': False,
            'is_bogon': False,
            'is_known_abuser': False,
            'is_known_attacker': False,
            'is_proxy': False,
            'is_threat': False,
            'is_tor': False},
 'time_zone': {'abbr': 'CST',
               'current_time': '2020-11-08T09:31:10.629425-06:00',
               'is_dst': False,
               'name': 'America/Chicago',
               'offset': '-0600'}}

Getting only one field

If you only need a single data attribute about an IP address you can extract it by passing a select_field parameter to the lookup method.

from ipdata import ipdata
from pprint import pprint
# Create an instance of an ipdata object. Replace `test` with your API Key
ipdata = ipdata.IPData('test')
response = ipdata.lookup('8.8.8.8', select_field='asn')
pprint(response)

Response

{'asn': {'asn': 'AS15169',
         'domain': 'google.com',
         'name': 'Google LLC',
         'route': '8.8.8.0/24',
         'type': 'hosting'},
 'status': 200

Getting a number of specific fields

If instead you need to get multiple specific fields you can pass a list of valid field names in a fields parameter.

from ipdata import ipdata
from pprint import pprint
# Create an instance of an ipdata object. Replace `test` with your API Key
ipdata = ipdata.IPData('test')
response = ipdata.lookup('8.8.8.8',fields=['ip','asn','country_name'])
pprint(response)

Response

{'asn': {'asn': 'AS15169',
         'domain': 'google.com',
         'name': 'Google LLC',
         'route': '8.8.8.0/24',
         'type': 'hosting'},
 'country_name': 'United States',
 'ip': '8.8.8.8',
 'status': 200}

Bulk Lookups

The API provides a /bulk endpoint that allows you to look up upto 100 IP addresses at a time. This is convenient for quickly clearing your backlog.

NOTE: Alternatively it is much simpler to process bulk lookups using the ipdata cli's batch command. All you need is a csv file with a list of IP addresses and you can get your results as either a JSON file or a CSV file! See the CLI Bulk Lookup Documentation for details.

from ipdata import ipdata
from pprint import pprint
# Create an instance of an ipdata object. Replace `test` with your API Key
ipdata = ipdata.IPData('test')
response = ipdata.bulk_lookup(['8.8.8.8','1.1.1.1'])
pprint(response)
Sample Response
{'responses': [{'asn': {'asn': 'AS15169',
                        'domain': 'google.com',
                        'name': 'Google LLC',
                        'route': '8.8.8.0/24',
                        'type': 'hosting'},
                'calling_code': '1',
                'city': None,
                'continent_code': 'NA',
                'continent_name': 'North America',
                'count': '1527',
                'country_code': 'US',
                'country_name': 'United States',
                'currency': {'code': 'USD',
                             'name': 'US Dollar',
                             'native': '$',
                             'plural': 'US dollars',
                             'symbol': '$'},
                'emoji_flag': '🇺🇸',
                'emoji_unicode': 'U+1F1FA U+1F1F8',
                'flag': 'https://ipdata.co/flags/us.png',
                'ip': '8.8.8.8',
                'is_eu': False,
                'languages': [{'name': 'English', 'native': 'English'}],
                'latitude': 37.751,
                'longitude': -97.822,
                'postal': None,
                'region': None,
                'region_code': None,
                'threat': {'is_anonymous': False,
                           'is_bogon': False,
                           'is_known_abuser': False,
                           'is_known_attacker': False,
                           'is_proxy': False,
                           'is_threat': False,
                           'is_tor': False},
                'time_zone': {'abbr': 'CST',
                              'current_time': '2020-11-08T09:34:45.362725-06:00',
                              'is_dst': False,
                              'name': 'America/Chicago',
                              'offset': '-0600'}},
               {'asn': {'asn': 'AS13335',
                        'domain': 'cloudflare.com',
                        'name': 'Cloudflare, Inc.',
                        'route': '1.1.1.0/24',
                        'type': 'hosting'},
                'calling_code': '61',
                'city': None,
                'continent_code': 'OC',
                'continent_name': 'Oceania',
                'count': '1527',
                'country_code': 'AU',
                'country_name': 'Australia',
                'currency': {'code': 'AUD',
                             'name': 'Australian Dollar',
                             'native': '$',
                             'plural': 'Australian dollars',
                             'symbol': 'AU$'},
                'emoji_flag': '🇦🇺',
                'emoji_unicode': 'U+1F1E6 U+1F1FA',
                'flag': 'https://ipdata.co/flags/au.png',
                'ip': '1.1.1.1',
                'is_eu': False,
                'languages': [{'name': 'English', 'native': 'English'}],
                'latitude': -33.494,
                'longitude': 143.2104,
                'postal': None,
                'region': None,
                'region_code': None,
                'threat': {'is_anonymous': False,
                           'is_bogon': False,
                           'is_known_abuser': False,
                           'is_known_attacker': False,
                           'is_proxy': False,
                           'is_threat': False,
                           'is_tor': False},
                'time_zone': {'abbr': 'AEDT',
                              'current_time': '2020-11-09T02:34:45.364564+11:00',
                              'is_dst': True,
                              'name': 'Australia/Sydney',
                              'offset': '+1100'}}],
 'status': 200}

Using the ipdata CLI

Available commands

ipdata --help
Usage: ipdata [OPTIONS] COMMAND [ARGS]...

  CLI for ipdata API

Options:
  --api-key TEXT  ipdata API Key
  --help          Show this message and exit.

Commands:
  batch
  info
  init
  me

Initialize the cli with your API Key

You need a valid API key from ipdata to use the cli. You can get a free key by Signing up here.

ipdata init <API Key>

You can also pass the --api-key <API Key> parameter to any command to specify a different API Key.

Look up your own IP address

Running the ipdata command without any parameters will look up the IP address of the computer you are running the command on. Alternatively you can explicitly look up your own IP address by running ipdata me . You can filter the JSON response with jq to get any specific fields you might be interested in.

ipdata

or

ipdata me

Using jq to filter the responses

ipdata me | jq .country_name

Look up an arbitrary IP address

You can pass any valid IPv4 or IPv6 address to the ipdata command to look it up. In case an invalid value is passed you will get the error Error: No such command "1..@1....1..1".

ipdata 8.8.8.8

Filter results by specifying comma separated list of fields

In case you don't want to use jq to filter responses to get specific fields you can instead pass a fields argument to the ipdata command along with a comma separated list of valid fields. Invalid fields are ignored. It is important to not include any whitespace in the list.

To access fields within nested objects eg. in the case of the asn, languages, currency, time_zone and threat objects, you can get a nested field by using dot notation with the name of the object and the name of the field. For example to get the time_zone name you would use time_zone.name, to get the time_zone abbreviation you would use time_zone.abbr

ipdata 8.8.8.8 --fields ip,country_code

Batch lookup

Perhaps the most useful command provided by the CLI is the ability to process a csv file with a list of IP addresses and write the results to file as either CSV or JSON! It could be a list of tens of thousands to millions of IP addresses and it will all be processed and the results filtered to your liking! When you use the JSON output format, the results are written to the output file you provide with one result per line. Each line being a valid and full JSON response object. If you only need a few fields eg. only the country name you can specify a field argument with the names of the fields you want, if you combine this with the CSV output format you will get very clean results with only the data you need!

To get full JSON responses for further processing

ipdata batch my_ip_backlog.csv --output geolocation_results.json

Batch lookup with output to CSV file

ipdata batch my_ip_backlog.csv --output results.csv --output-format CSV --fields ip,country_code

The --fields option is required in case of CSV output.

Example Results

# ip,country_code
107.175.75.83,US
35.155.95.229,US
13.0.0.164,US
209.248.120.14,US
142.0.202.238,US
...

Available Fields

A list of all the fields returned by the API is maintained at Response Fields

Errors

A list of possible errors is available at Status Codes

Tests

To run all tests

python -m unittest

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipdata-3.4.3.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ipdata-3.4.3-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file ipdata-3.4.3.tar.gz.

File metadata

  • Download URL: ipdata-3.4.3.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for ipdata-3.4.3.tar.gz
Algorithm Hash digest
SHA256 2a4a57cd058a9dc45f29c027d94e334063ef5610ccdf588794f323d20836f599
MD5 202b5fd2bd3a6f35443bc2609b000f71
BLAKE2b-256 6114715f1dbc0c46ead795995ce68590f967944b8f3d7572599714b7992de776

See more details on using hashes here.

File details

Details for the file ipdata-3.4.3-py3-none-any.whl.

File metadata

  • Download URL: ipdata-3.4.3-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for ipdata-3.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e8de39340d6c9c186ac7a515a575ba44be7dc7f46bd8fb221296237c57cb06ba
MD5 7dcf2c4541358825f90da0fdfaf1c8f3
BLAKE2b-256 a0caf2c9ad392ae6001aa8a75591f8b0eb14d398cf3e1da51b98e2f3dd8b1db4

See more details on using hashes here.

Release history Release notifications | RSS feed

4.0.9

2 files

4.0.8

2 files

4.0.7

2 files

4.0.6

2 files

4.0.5

2 files

4.0.4

2 files

4.0.3

2 files

4.0.0

2 files

3.4.6

2 files

3.4.4

2 files

This release

3.4.3 This release

2 files

3.4.2

2 files

3.4.1

2 files

3.4.0

2 files

3.3.9

2 files

3.3.8

2 files

3.3.6

2 files

3.3.1

2 files

3.2

1 file

3.1

1 file

3.0

1 file

2.8

1 file

2.6

1 file

2.5

1 file

2.4

1 file

2.3

1 file

2.2

1 file

2.1

1 file

2.0

1 file

1.9

1 file

1.8

1 file

1.7

1 file

1.6

1 file

1.5

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page