使用Python查找网络(外部)IP地址 [英] Finding network (external) IP addresses using Python
问题描述
我想通过Python了解我的互联网提供商(外部)IP地址(宽带或其他)。
I want to know my internet provider (external) IP address (broadband or something else) with Python.
有多台机器连接到该网络。我尝试了不同的方式,但我只获得本地和公共IP我的机器。如何通过Python找到我的外部IP地址?
There are multiple machines are connected to that network. I tried in different way's but I got only the local and public IP my machine. How do I find my external IP address through Python?
提前致谢。
推荐答案
使用此脚本:
import urllib, json
data = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())
print data["ip"]
没有json:
import urllib, re
data = re.search('"([0-9.]*)"', urllib.urlopen("http://ip.jsontest.com/").read()).group(1)
print data
其他方式是解析ifconfig(= linux)或ipconfig(= windows)命令但要注意翻译的Windows系统
(ipconfig已翻译)。
Other way it was to parse ifconfig (= linux) or ipconfig (= windows) command but take care with translated Windows System (ipconfig was translated).
linux的lib示例:
ifparser 。
Example of lib for linux : ifparser.
这篇关于使用Python查找网络(外部)IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!