ipdiscover.py 文件源码

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

项目:p2pool-unitus 作者: amarian12 项目源码 文件源码
def _discover_multicast():
    """
    Local IP discovery protocol via multicast:
        - Broadcast 3 ping multicast packet with "ping" in it
        - Wait for an answer
        - Retrieve the ip address from the returning packet, which is ours
    """

    nonce = str(random.randrange(2**64))
    p = _LocalNetworkMulticast(nonce)

    for attempt in itertools.count():
        port = 11000 + random.randint(0, 5000)
        try:
            mcast = reactor.listenMulticast(port, p)
        except CannotListenError:
            if attempt >= 10:
                raise
            continue
        else:
            break

    try:
        yield mcast.joinGroup('239.255.255.250', socket.INADDR_ANY)

        logging.debug("Sending multicast ping")
        for i in xrange(3):
            p.transport.write(nonce, ('239.255.255.250', port))

        address, = yield p.address_received.get_deferred(5)
    finally:
        mcast.stopListening()

    defer.returnValue(address)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号