Skip to main content

A generic library for getting your externally visible IP address in python.

Project description

A generic library in python to get your external IP from sites around the internet in a consistent manner. Using zopes interfaces, allows the user to create their own sources. Features time-based caching, multi-source verification and other features.

Using IP Providers

IP providers are the high-level interfaces to IPSources. They allow the user an abstraction from the source provider and in fact allow them to use multiple sources transparently.

By default all providers cache a response from one hour (3600 seconds) and will self-invalidate in the event that time passes or the users requests an additional information key that is not present in the current cache.

Note: because different sources return different supplement information to the IP address, the users must know the key for which they’re looking in order to make use of the information. This is one of the rougher edges of the code base because it is on the boundary of the intended scope. If you need a feature, please ask for it.

IPProvider

The standard IPProvider checks a single source, provides and caches the answer.

In [1]: import echoip
In [2]: import echoip.sources
In [3]: import echoip.providers
In [4]: source_factory = echoip.sources.IPSourceFactory()
In [5]: provider = echoip.providers.IPProvider()
In [6]: for source in source_factory.get_sources():
   ...:     provider.add_source(source)
   ...:
In [7]: provider.get_ip()
Out[7]: IPv4Address('67.171.19.153')

Usage:

Type:            type
String form:     <class 'echoip.providers.IPProvider'>
Init definition: echoip.providers.IPProvider(self, source_list=None, cache_ttl=3600)
Init docstring:
The IPProvider takes one or more IPSources and returns the results from them . If one
provider does not respond it will move on to the next. It ensures that the age of the
response on no older than the cache_ttl.

:param source_list: The list of sources to bootstrap the provider with
:type source_list: list of IIPSource providers
:param cache_ttl: the seconds the ip cache remains valid
:type cache_ttl: int

Some sources choose to provide additional information (like GeoIP information). That information is marshalled into a single dictionary based and can be retrieved by get_info():

In [8]: provider.get_info()
Out[8]:
{u'city': u'Keb',
 u'country': u'CO',
 u'loc': u'27.6355,-22.3235'}

Further documenation:

MultipleSourceIPProvider

The MultipleSourceIPProvider checks with multiple sources. If and only if min_source_agreement (constructor parameter) sources agree on the IP address is the answer cached.

Note: The IP is cached. The additional info from the sources are combined into a single dictionary. Duplicate keys are overwritten in a non-deterministic fashion. This provider is mostly useful for when you are distrustful of the IP results.

The provider is used the exact same way.

In [1]: import echoip
In [2]: import echoip.sources
In [3]: import echoip.providers
In [4]: source_factory = echoip.sources.IPSourceFactory()
In [5]: provider = echoip.providers.MultisourceIPProvider
In [6]: for source in source_factory.get_sources():
   ...:     provider.add_source(source)
   ...:
In [7]: provider.get_ip()
Out[7]: IPv4Address('67.171.19.153')

Usage:

Type:            type
String form:     <class 'echoip.providers.MultisourceIPProvider'>
File:            /Users/eflee/Development/pyechoip/src/echoip/providers.py
Init definition: echoip.providers.MultisourceIPProvider(self, source_list=None, cache_ttl=3600, min_source_agreement=2)
Init docstring:
The IPProvider takes one or more IPSources and returns the results from them if the minimum number of sources
are in agreement regard the external IP.
Additional info from multiple providers is extended into one dictionary.
If one provider does not respond it will move on to the next. It ensures that the age of the
response on no older than the cache_ttl.

:param source_list: The list of sources to bootstrap the provider with
:type source_list: list of IIPSource providers
:param cache_ttl: the seconds the ip cache remains valid
:param min_source_agreement: The minimum number of source that must be in agreement for an ip_fetch
:type min_source_agreement: int

Further documenation:

Using IP Sources

IPSources are the low-level interface to the library. There is no caching for IPSources and the only validation is the returned IP address.

There are two (as of now) basic types: SimpleIPSources and JSONIPSources which seem to be the two most common interface types for the publicly available services.

In addition to IPSources there is an IPSourceFactory that can generate configured sources for the list of built-ins below.

Finally, there is an IIPSource interface for implementing customer sources.

IIPSource (Interface)

I want to cover this in brief, this is the interface for implementing custom sources. It is rather simple: the source must provide an ip attribute and an info attribute.

The ip attribute is the ipaddress.IPv4Address or ipaddress.IPv6Address (py2-ipaddress for Python2 or ipaddress for Python3) encapsulated ip address returned from the API.

The info attribute is a key-value dictionary for all other results returned from the API. There is no additional guarantees of cleanliness or format and this varries from source to source.

SimpleIPSource

The SimpleIPSource handles sites like curlmyip.com that return only an string IP. The class simply strips the whitespace from the string and validates it by instantiating it as an IPAddress.

In [1]: import echoip.sources
In [2]: source = echoip.sources.SimpleIPSource('http://curlmyip.com/')
In [3]: source.ip
Out[3]: IPv4Address('67.171.19.153')

The info attribute on this IPSource type is always empty.

JSONIPSource

The JSONIPSource handles sites like ip-api.com that provide a more complete API (obviousely a JSON API). The class takes a json key to select the IP and then uses the same validation as the SimpleIPSource class. All other results returned by the API are boxed into a dict that is returned by the info attribute.

In [1]: import echoip.sources
In [2]: source = echoip.sources.JSONIPSource('http://ip-api.com/json', 'query')
In [3]: source.ip
Out[3]: IPv4Address('67.171.19.153')
In [4]: source.info
Out[4]:
{u'city': u'Keb',
 u'country': u'CO',
 u'loc': u'27.6355,-22.3235'}

IPSourceFactory

The IPSourceFactory is a utility class that has two purposes. One, it allows the user to create instances based on their own resources using the provided source types. Two, it allows the user to instantiate with all of the built-in sites.

Instantiating with custom resources:

In [1]: import echoip.sources
In [2]: fac = echoip.sources.IPSourceFactory(use_builtins=False)
In [3]: fac.add_source(echoip.sources.SimpleIPSource, 'http://10.0.0.1')
In [4]: fac.add_source(echoip.sources.JSONIPSource, 'http://10.0.0.2', 'ip')
In [5]: fac.num_sources
Out[5]: 2
In [6]: [ src for src in fac.get_sources() ]
Out[6]:
[<echoip.sources.SimpleIPSource at 0x10bb5d390>,
<echoip.sources.JSONIPSource at 0x10bb5d3d0>]

Instantiation with built-ins (default):

In [1]: import echoip.sources
In [2]: fac = echoip.sources.IPSourceFactory()
In [3]: fac.num_sources
Out[3]: 14
In [4]: [ src for src in fac.get_sources() ]
Out[4]:
[<echoip.sources.JSONIPSource at 0x102f6b510>,
<echoip.sources.JSONIPSource at 0x102f6b550>,
<echoip.sources.JSONIPSource at 0x102f6b590>,
...
<echoip.sources.SimpleIPSource at 0x102f6b850>]

Built-in Sources

Users of this library who choose to use the default sources are subject to the terms and limitations of those sources and are responsible for their conduct when using these services.

PyEchoIP Thanks

This library is of my own creation. However in order to make it immediately useful to its users, I have included in the code several ‘default’ providers that are well known and publicly available on the internet.

These services come in various forms, some providing detailed GEOIP information and some providing simple IPv4 information. I’ve attempted to provide sources whose usage terms do not prohibit inclusion or who do not provide such terms. I have contacted the maintainers of these APIs via contact instruction on each website or via whois to ask for permission.

I have also attempted to create a library that will not abuse providers.

If you are a provider and would like to be removed from inclusion, please file a github issue.

Sources providing free and pro versions

(If you need further RPS, SSL connection or would like to contribute cash, get a license)

  • ip-api.com

  • ipinfo.io

Sources providing free APIs

Project details


Download files

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

Source Distribution

pyechoip-1.3.tar.gz (9.5 kB view details)

Uploaded Source

Built Distributions

pyechoip-1.3-py3.4.egg (14.2 kB view details)

Uploaded Source

pyechoip-1.3-py2.7.egg (13.8 kB view details)

Uploaded Source

File details

Details for the file pyechoip-1.3.tar.gz.

File metadata

  • Download URL: pyechoip-1.3.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyechoip-1.3.tar.gz
Algorithm Hash digest
SHA256 ba7841e597b3fe0ea5f7c798693d49bc097b37b9b23765452e1c8a435af0b8c1
MD5 2bb958a53fb95370da50de7817ac1ae7
BLAKE2b-256 e4c2d5413cf9a1536261baf1b2e24e162c832dece030b9bb696b2f16aa7f5a63

See more details on using hashes here.

File details

Details for the file pyechoip-1.3-py3.4.egg.

File metadata

  • Download URL: pyechoip-1.3-py3.4.egg
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyechoip-1.3-py3.4.egg
Algorithm Hash digest
SHA256 afb8d15520b3282263c5f7f252484a7ff5f598dd03266b069e429d85b4248894
MD5 672b7e25507c279e617676afddb8dc4e
BLAKE2b-256 75b6a30794133f81dbd58e311bc854755f6a7a01af61e9d3c7ce384bc8b8158c

See more details on using hashes here.

File details

Details for the file pyechoip-1.3-py2.7.egg.

File metadata

  • Download URL: pyechoip-1.3-py2.7.egg
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyechoip-1.3-py2.7.egg
Algorithm Hash digest
SHA256 cb78edb55fe82ccaa36e9eeb48f49eeed97b945d532aa15a50ce4bd1e1adde67
MD5 24fecf822321a48a2ccf545705fc85b2
BLAKE2b-256 099f154cdd9a889c72a166b417c260c6aa4f6f0dec1885769d2b525185a1e5ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page