How can I get the public IP using python2.7? Not private IP.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Currently there are several options:
Below are exact ways you can utilize each of the above.
ip.42.pl
This is the first option I have found. It is very convenient for scripts, you don't need JSON parsing here.
jsonip.com
Seemingly the sole purpose of this domain is to return IP address in JSON.
httpbin.org
httpbin.org is service I often recommend to junior developers to use for testing their scripts / applications.
ipify.org
Power of this service results from lack of limits (there is no rate limiting), infrastructure (placed on Heroku, with high availability in mind) and flexibility (works for both IPv4 and IPv6).
EDIT: Added httpbin.org to the list of available options.
EDIT: Added ipify.org thanks to kert's note.
This is a way not to have to make a call to the internet:
Please let me know if this doesn't work, then I can update the answer (it works for ~10 servers of mine)
I like the requests package with http://ip.42.pl/raw
You can just do this:
Produces:
Getip is a small module which returns public IP address from a random server.
Install:
Use:
Try this: