How can I get the public IP using python2.7?

后端 未结 7 1328
梦毁少年i
梦毁少年i 2020-11-30 22:43

How can I get the public IP using python2.7? Not private IP.

相关标签:
7条回答
  • 2020-11-30 23:17

    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"]
    
    0 讨论(0)
提交回复
热议问题