使用 Python 获取机器的外部 IP 地址 [英] Getting a machine's external IP address with Python

查看:44
本文介绍了使用 Python 获取机器的外部 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找一种更好的方法来获取机器当前的外部 IP #...下面的工作,但宁愿不依赖外部站点来收集信息...我被限制使用捆绑的标准 Python 2.5.1 库使用 Mac OS X 10.5.x

Looking for a better way to get a machines current external IP #... Below works, but would rather not rely on an outside site to gather the information ... I am restricted to using standard Python 2.5.1 libraries bundled with Mac OS X 10.5.x

import os
import urllib2

def check_in():

    fqn = os.uname()[1]
    ext_ip = urllib2.urlopen('http://whatismyip.org').read()
    print ("Asset: %s " % fqn, "Checking in from IP#: %s " % ext_ip)

推荐答案

如果您在获取外部 IP 的路由器后面,恐怕您别无选择,只能像您一样使用外部服务.如果路由器本身有一些查询接口,您可以使用它,但解决方案将非常特定于环境且不可靠.

If you are behind a router which obtains the external IP, I'm afraid you have no other option but to use external service like you do. If the router itself has some query interface, you can use it, but the solution will be very environment-specific and unreliable.

这篇关于使用 Python 获取机器的外部 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆