• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    Python
  • License
    The Unlicense
  • Created over 9 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The official client library for ipify: A Simple IP Address API.

python-ipify

The official client library for ipify: A Simple IP Address API.

python-ipify Release python-ipify Downloads python-ipify Build https://coveralls.io/repos/rdegges/python-ipify/badge.svg?branch=master

Meta

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

Not yet released.

  • 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.

Version 1.0.0

Released May 6, 2015.

  • First release!

More Repositories

1

ipify-api

A public IP API service.
Go
1,737
star
2

django-skel

A modern Django (1.5) project skeleton.
Python
518
star
3

django-sslify

Force SSL on your Django site.
Python
336
star
4

django-twilio

A simple library for building twilio-powered Django webapps.
Python
327
star
5

skele-cli

A skeleton command line program in Python.
Python
293
star
6

simpleq

A simple, infinitely scalable, SQS based queue.
Python
165
star
7

flask-dynamo

DynamoDB integration for Flask.
Python
141
star
8

cryptocompare

A static website that compares cryptocurrency prices for popular currencies.
HTML
123
star
9

chatapp

A simple WebRTC and Okta front-end app.
JavaScript
122
star
10

go-ipify

The official client library for ipify: A Simple IP Address API.
Go
119
star
11

ss-auth

Stupid Simple Authentication
JavaScript
110
star
12

python-amazonify

The simplest way to build Amazon Affiliate links, in Python.
Python
104
star
13

brute

Simple brute forcing in Python.
Python
101
star
14

django-heroku-memcacheify

Automatic Django memcached configuration on Heroku.
Python
100
star
15

svcc-auth

Demo code for my Silicon Valley Code Camp talk about authentication in Node.js!
JavaScript
97
star
16

pycall

A flexible python library for creating and using Asterisk call files.
Python
92
star
17

django-clear-cache

A simple Django management command which clears your cache.
Python
88
star
18

django-ganalytics

Simple Google Analytics integration for Django.
Python
48
star
19

pelican-minify

An HTML minification plugin for Pelican, the static site generator.
Python
47
star
20

django-heroku-postgresify

Automatic Django database configuration on Heroku.
Python
46
star
21

btc

Buy, sell, and transfer bitcoin instantly in your terminal!
Python
45
star
22

flask-heroku-cacheify

Automatic Flask cache configuration on Heroku.
Python
41
star
23

python-basicauth

An incredibly simple HTTP basic auth implementation.
Python
36
star
24

useragent-api

A random user agent API service.
Python
34
star
25

energy-tracker

A simple script (meant to be run via cron) that tracks your Mac laptop's energy usage in watt hours.
Shell
33
star
26

dot-tmux

My tmux dotfiles.
21
star
27

dot-vim

My vim dotfiles.
Vim Script
16
star
28

rdegges-www

My personal website and blog.
SCSS
10
star
29

flask-simple

SimpleDB integration for Flask.
Python
10
star
30

flask-heroku-rqify

Automatic RQ configuration for your Heroku Flask applications.
Python
10
star
31

dot-git

My git dotfiles.
8
star
32

dot-zsh

My ZSH dotfiles.
Shell
6
star
33

rdegges-blog

My personal writings.
6
star
34

cagi

An Asterisk 1.6+ compatible AGI library in C.
C
5
star
35

dog-name-picker

A Python script I used to pick my dog's name.
Python
4
star
36

node-camels

Easily convert to and from camelCase and under_score notation.
JavaScript
3
star
37

skele

A simple, modern, Express 4.x skeleton.
JavaScript
2
star
38

django-coverage

Fork of the popular django-coverage package.
2
star
39

dot-pip

My pip dotfiles.
2
star
40

bitrich-www

HTML
2
star
41

ice-breaker

A simple cross-platform CLI tool that destroys Amazon Glacier vaults.
Go
1
star
42

iterate-banner

Code I used to generate a cool opening video at the Iterate conference in 2018.
Python
1
star
43

dsobook.club

Source code for the DevSecOps Book Club website.
HTML
1
star
44

rondegges-www

My dad's website.
CSS
1
star
45

postgression-www

The public facing website for postgression.com.
1
star
46

dot-python

Dotfiles for managing Python.
Shell
1
star
47

codehappy-www

The public facing Code Happy website.
Pug
1
star
48

sofind

A simple CLI tool to find good StackOverflow users.
JavaScript
1
star
49

actiontest

testing...
1
star
50

generator-momma

yeoman generator for yo momma
JavaScript
1
star
51

python-data247

An API client for Data 24-7.
Python
1
star