A fork from the official client library for ipify: A Simple IP Address API.
Project description
A fork from the official client library for ipify: A Simple IP Address API.
Meta
Author: Simone Perini
Email: perinisimone98@gmail.com
Original Author: Randall Degges
Original Author’s Email: r@rdegges.com
Original Author’s Site: http://www.rdegges.com
Status: maintained, active
Purpose
ipify is the best IP address lookup service on the internet. It’s fast, simple, scalable, open source, and well-funded (by me!).
In short: if you need a way to pragmatically get your public IP address, ipify is the best possible choice!
This library will retrieve your public IP address from ipify’s API service, and return it as a string. It can’t get any simpler than that.
This library also has some other nice features you might care about:
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 ipify, simply run:
$ pip install ipify
This will install the latest version of the library automatically.
Usage
Using this library is very simple. Here’s a simple example:
>>> from ipify import get_ip
>>> ip = get_ip()
>>> ip
u'96.41.136.144'
Now, in regards to exception handling, there are several ways this can fail:
The ipify service is down (not likely), or:
Your machine is unable to get the request to ipify because of a network error of some sort (DNS, no internet, etc.).
Here’s how you can handle all of these edge cases:
from ipify import get_ip
from ipify.exceptions import ConnectionError, ServiceError
try:
ip = get_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, you could also do the following (it will catch any sort of ipify related errors regardless of what type they may be):
from ipify import get_ip
from ipify.exceptions import IpifyException
try:
ip = get_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.
Contributing
This project is only possible due to the amazing contributors who work on it!
If you’d like to improve this library, please send me a pull request! I’m happy to review and merge pull requests.
The standard contribution workflow should look something like this:
Fork this project on Github.
Make some changes in the master branch (this project is simple, so no need to complicate things).
Send a pull request when ready.
Also, if you’re making changes, please write tests for your changes – this project has a full test suite you can easily modify / test.
To run the test suite, you can use the following commands:
$ pip install -e .
$ pip install -r requirements.txt
$ python setup.py test
Change Log
All library changes, in descending order.
Version 1.0.1
Released January 26, 2022.
Improving test to actually validate IP addresses. Thanks to @lethargilistic for the pull request!
Fixing URLs in the README / comments to point to https URLs. Thanks to @ktdreyer for the pull request!
Fixing typo in the README. Thanks @prologic for the find!
Adding a working test for exercising ServiceError exceptions. Improves test coverage a bit =)
Removing unnecessary assertions / tests.
Adding test to improve test coverage to 100% =)
Fixing minor style issues. I’m really obsessed with code style / quality, don’t judge me!
Adding Python 3.5 / 3.6 support.
Adding Python 3.10 support.
Version 1.0.0
Released May 6, 2015.
First release!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ipify-py-1.0.1.tar.gz
.
File metadata
- Download URL: ipify-py-1.0.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44bf2dcad7791b8f53ff7b00563f48d6b3e3464b90a1a1fd8f68297f084c266f |
|
MD5 | 057fd05ba45f96432ac13162e7d19530 |
|
BLAKE2b-256 | 0dd77219441d38b4f7e07585c98e118fadba8b69012b0f0e69ad3b47c893183e |
File details
Details for the file ipify_py-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: ipify_py-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50eb018c41b20e275a8c9a62c7ea70d1fd0ca9744ba126442e2ab42d9955f81f |
|
MD5 | 79261c3b783fc2031f4e0389024b0761 |
|
BLAKE2b-256 | fa712eca0eba297e09f5d01eb373b114124e01a784048b3e2343c5c097d6b20a |