查看本机局域网ip

这样可以获取到本机所有网卡的IP地址:
IPs = socket.gethostbyname_ex(socket.gethostname())[-1]
如果想获取正在上网所使用的本机IP,通过route命令可以得到:
Windows下用[a for a in os.popen('route print').readlines() if ' 0.0.0.0 ' in a][0].split()[-2]
Linux下用
[a for a in os.popen('/sbin/route').readlines() if 'default' in a][0].split()[1]

原文链接:https://www.zhihu.com/question/49036683/answer/114986702

posted @ 2021-06-28 13:40  pythoner_wl  阅读(248)  评论(0编辑  收藏  举报