예제 #1
0
def get_deferred_host_ip():
    global _host_ip
    global _host_ip_callbacks
    global _host_ip_cachetime
    if hasattr(reactor, 'ident'):
        assert reactor.ident == thread.get_ident()

    if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime():
        return defer.succeed(_host_ip)

    if get_route_ip:
        ip = get_route_ip()
        if ip:
            _host_ip = ip
            _host_ip_cachetime = bttime()
            return defer.succeed(_host_ip)            

    df = defer.Deferred()
    
    if not _host_ip_callbacks:
        def connect(ip):
            factory = RecorderFactory()
            factory.protocol = RecorderProtocol
            if hasattr(reactor, 'limiter'):
                reactor.connectTCP(ip, 80, factory, urgent=True)
            else:
                reactor.connectTCP(ip, 80, factory)            
        rdf = reactor.resolve("ip.bittorrent.com")
        rdf.addCallback(connect)
        rdf.addErrback(lambda e : _got_result(None))
    
    _host_ip_callbacks.append(df)

    return df
예제 #2
0
def get_host_ip():
    """ Blocking version, do not use from reactor thread! """
    global _host_ip
    global _host_ip_callbacks
    global _host_ip_cachetime
    if hasattr(reactor, 'ident'):
        assert reactor.ident != thread.get_ident()

    if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime():
        return _host_ip

    if get_route_ip:
        ip = get_route_ip()
        if ip:
            _host_ip = ip
            _host_ip_cachetime = bttime()
            return _host_ip

    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(5)
        
        # what moron decided to make try/except/finally not work?
        # Guido van Rossum.
        try:
            s.connect(("ip.bittorrent.com", 80))
            endpoint = s.getsockname()
            _host_ip = endpoint[0]
            _host_ip_cachetime = bttime()
            s.send("GET /myip HTTP/1.0\r\n\r\n")
        except (socket.error, socket.timeout), e:
            try:
                _host_ip = socket.gethostbyname(socket.gethostname())
            except socket.error, e:
                pass
예제 #3
0
def get_deferred_host_ip():
    global _host_ip
    global _host_ip_callbacks
    global _host_ip_cachetime
    if hasattr(reactor, 'ident'):
        assert reactor.ident == thread.get_ident()

    if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime(
    ):
        return defer.succeed(_host_ip)

    if get_route_ip:
        ip = get_route_ip()
        if ip:
            _host_ip = ip
            _host_ip_cachetime = bttime()
            return defer.succeed(_host_ip)

    df = defer.Deferred()

    if not _host_ip_callbacks:

        def connect(ip):
            factory = RecorderFactory()
            factory.protocol = RecorderProtocol
            if hasattr(reactor, 'limiter'):
                reactor.connectTCP(ip, 80, factory, urgent=True)
            else:
                reactor.connectTCP(ip, 80, factory)

        rdf = reactor.resolve("ip.bittorrent.com")
        rdf.addCallback(connect)
        rdf.addErrback(lambda e: _got_result(None))

    _host_ip_callbacks.append(df)

    return df
예제 #4
0
def get_host_ip():
    """ Blocking version, do not use from reactor thread! """
    global _host_ip
    global _host_ip_callbacks
    global _host_ip_cachetime
    if hasattr(reactor, 'ident'):
        assert reactor.ident != thread.get_ident()

    if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime(
    ):
        return _host_ip

    if get_route_ip:
        ip = get_route_ip()
        if ip:
            _host_ip = ip
            _host_ip_cachetime = bttime()
            return _host_ip

    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(5)

        # what moron decided to make try/except/finally not work?
        # Guido van Rossum.
        try:
            s.connect(("ip.bittorrent.com", 80))
            endpoint = s.getsockname()
            _host_ip = endpoint[0]
            _host_ip_cachetime = bttime()
            s.send("GET /myip HTTP/1.0\r\n\r\n")
        except (socket.error, socket.timeout), e:
            try:
                _host_ip = socket.gethostbyname(socket.gethostname())
            except socket.error, e:
                pass