Skip to main content

Get IP address information via ipify.org

Project description

ipify2

An updated unofficial clone of the now-deprecated ipify library, allowing you to determine your IPv4 and IPv6 programmatically via ipify.org

.. image:: https://img.shields.io/pypi/v/ipify2.svg :alt: ipify2 Release :target: https://pypi.python.org/pypi/ipify2

.. image:: https://img.shields.io/pypi/dm/ipify2.svg :alt: ipify2 Downloads :target: https://pypi.python.org/pypi/ipify2

Meta

Original

Maintainer

Purpose

ipify.org is a reliable IP address lookup service, an easy way to get your public IP address in Python.

This library will retrieve your public IP address from ipify's API service, and return it as a string.

Additional features:

  • If a request fails for any reason, it is re-attempted 3 times using an exponential backoff algorithm for maximum effectiveness.
  • This library handles exceptions properly, and usage examples below show you how to deal with errors in a foolproof way.
  • This library only makes API requests over HTTPS.

Installation

To install ipify2, simply run:

pip install ipify2

This will install the latest version of the library automatically.

Usage

Using this library is very simple. Here's a simple example:

from ipify2 import get_ipv4

ip = get_ipv4()
print(ip) # '96.41.136.144'
from ipify2 import get_ipv6
ip = get_ipv6()
print(ip) # '2001:0db8:85a3:0000:0000:8a2e:0370:7334'

Error Handling

There are several reasons a request fail:

  • The ipify service is down
  • Your machine is unable to get the request to ipify because of a network error of some sort (DNS, no internet, etc.).

To handle these errors, you can do the following:

from ipify2 import get_universal_ip
from ipify2.exceptions import ConnectionError, ServiceError

try:
    ip = get_universal_ip()
except ConnectionError:
    # If you get here, it means you were unable to reach the ipify service,
    # most likely because of a network error on your end.
except ServiceError:
    # If you get here, it means ipify is having issues, so the request
    # couldn't be completed :(
except:
    # Something else happened (non-ipify related). Maybe you hit CTRL-C
    # while the program was running, the kernel is killing your process, or
    # something else all together.

If you want to simplify the above error handling by catching all errors, you could also do the following:

from ipify2 import get_universal_ip
from ipify2.exceptions import IpifyException

try:
    ip = get_universal_ip()
except IpifyException:
    # If you get here, then some ipify exception occurred.
except:
    # If you get here, some non-ipify related exception occurred.

One thing to keep in mind: regardless of how you decide to handle exceptions, the ipify library will retry any failed requests 3 times before ever raising exceptions -- so if you do need to handle exceptions, just remember that retry logic has already been attempted.

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

ipify2-1.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

ipify2-1.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file ipify2-1.1.0.tar.gz.

File metadata

  • Download URL: ipify2-1.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for ipify2-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4210044443ee3924a41d38a33ee138e64d495deeec903cb08ba76696d7a77f35
MD5 76ab0321d843edfe87ec18b85d505e34
BLAKE2b-256 8e99037f7c32de8ac892b4479266890ab73b94e44e24f4093b424c8877884b91

See more details on using hashes here.

File details

Details for the file ipify2-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ipify2-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for ipify2-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7bf03bb7984ce14782a7cb707a77d6c777b97f5c06a2f0939a8a130de674462e
MD5 893cbade6455c20886ef87b36e48d66a
BLAKE2b-256 ed1110fe882cd3b49344245b778e7285b8a25237ebb57dd4a0f6c9099c71e608

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