How can I get the public IP using python2.7? Not private IP.
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)
from subprocess import check_output
out = check_output("/sbin/ifconfig | awk '/inet / { print $2 }' | sed 's/addr://'", shell=True)
[x for x in out.decode().split() if not x == "127.0.0.1" and
not (x.startswith("172") and x.endswith("0.1"))]
["x.x.x.x.x"]