Python Package - whatismyip
Posted by Al Sweigart in misc
Back in August 2020, I created a Python package whatismyip that can tell your Python script your public IP address using free, no-registration online services. This blog post covers the package after a recent update. The source code is available in a git repo. and you can download it with pip or other tools from PyPI. If you are interested in this package, I also have a whereismyip Python package that does geolocation based on IP address. To find out your IP address, run import whatismyip and call whatismyip.whatismyip() to return a string of the IPv4 or IPv6 address. There are additional functions for more specific requests. You can also run python -m whatismyip.
The whatismyip name does not use snakecase because it's named after the whatismyip.com website, an early IP address finding service. For consistency, the other functions are named similiarly.
Here is some example usage:
>>> import whatismyip
>>> whatismyip.amionline()
True
>>> whatismyip.whatismyip()
'69.89.31.226'
>>> whatismyip.whatismyipv4()
'69.89.31.226'
>>> whatismyip.whatismyipv6()
'2345:0425:2CA1:0000:0000:0567:5673:23b5'
>>> whatismyip.whatismylocalip() # Returns local IPs of all network cards.
('192.168.189.1', '192.168.220.1', '192.168.56.1', '192.168.1.201')
>>> whatismyip.whatismyhostname()
'GIBSON'
There are several public STUN (Session Traversal Utilities for NAT) servers (listed in the whatismyip.STUN_SERVERS variable, in order of response speed) that return your IP address (as it appears to them). There are also several public websites that you can view in your browser (listed in whatismyip.IP4_WEBSITES, whatismyip.IP6_WEBSITES, and whatismyip.IP_WEBSITES):
- https://api.ipify.org
- https://checkip.amazonaws.com/
- https://icanhazip.com
- https://ipinfo.io/ip
- https://ipecho.net/plain
- https://ifconfig.me/ip
- https://v4.ident.me
- https://v6.ident.me
- https://v4.tnedi.me
- https://v6.tnedi.me
- https://tnedi.me/
- https://curlmyip.net
- https://wtfismyip.com/text
The whatismyip module does not have any dependencies outside the Python standard library. It does not require Requests to be installed.