A Python function to get your public IP address.
Installation
pip install public-ip
Usage example
import public_ip as ip
ip.get()
Sample output:
212.51.139.31
How it works
The function queries in parallel six different websites...
- https://api.ipify.org
- https://checkip.amazonaws.com
- https://icanhazip.com
- https://ifconfig.co/ip
- https://ipecho.net/plain
- https://ipinfo.io/ip
... and returns the most common IP among the responses. In this manner (a) we don't depend on a single external service and (b) we can determine the IP correctly as long as a majority of these sites return the right value.