upnp.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:server 作者: happypandax 项目源码 文件源码
def _get_local_ips():
    local_ips = []

    # get local ip using UDP and a  broadcast address
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    # Not using <broadcast> because gevents getaddrinfo doesn't like that
    # using port 1 as per hobbldygoop's comment about port 0 not working on osx:
    # https://github.com/sirMackk/ZeroNet/commit/fdcd15cf8df0008a2070647d4d28ffedb503fba2#commitcomment-9863928
    s.connect(('239.255.255.250', 1))
    local_ips.append(s.getsockname()[0])

    # Get ip by using UDP and a normal address (google dns ip)
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 0))
        local_ips.append(s.getsockname()[0])
    except BaseException:
        pass

    # Get ip by '' hostname . Not supported on all platforms.
    try:
        local_ips += socket.gethostbyname_ex('')[2]
    except BaseException:
        pass

    # Delete duplicates
    local_ips = list(set(local_ips))

    logging.debug("Found local ips: %s" % local_ips)
    return local_ips
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号