예제 #1
0
파일: lib.py 프로젝트: sahwar/lanshark
def resolve(addr):
    """resolve host if enabled in config"""
    if config.RESOLVE_HOSTS:
        try:
            return socket.gethostbyaddr(addr)[0]
        except socket.herror, e:
            logger.debug(e)
    return addr


def discover(async=False):
    """Discover other hosts in the network"""
    for item in config.STATICHOSTS:
        url = "http://%s/" % item
        yield (item, url)
    s = network.broadcast_dgram_socket(config.CLIENT_PORT)
    hello = config.NETWORK_NAME
    s.sendto(hello, (config.BROADCAST_IP, config.PORT))
    for data in recv(s, config.DISCOVER_TIMEOUT, async):
        if data:
            msg, (addr, port) = data
            if config.STATICHOSTS and\
                    "%s:%i" % (addr, port) in config.STATICHOSTS:
                continue
            if msg.startswith(hello):
                name = msg[len(hello) + 1:]
                url = "http://%s:%i/" % (resolve(addr), port)
                yield (name, url)
            else:
                yield None
        else:
예제 #2
0
파일: daemon.py 프로젝트: sahwar/lanshark
 def __init__(self, fi):
     threading.Thread.__init__(self)
     self.setDaemon(True)
     self.fileindex = fi
     self.socket = network.broadcast_dgram_socket(config.PORT)
예제 #3
0
파일: lib.py 프로젝트: imclab/lanshark
def resolve(addr):
    """resolve host if enabled in config"""
    if config.RESOLVE_HOSTS:
        try:
            return socket.gethostbyaddr(addr)[0]
        except socket.herror, e:
            logger.debug(e)
    return addr

def discover(async=False):
    """Discover other hosts in the network"""
    for item in config.STATICHOSTS:
        url = "http://%s/" % item
        yield (item, url)
    s = network.broadcast_dgram_socket(config.CLIENT_PORT)
    hello = config.NETWORK_NAME
    s.sendto(hello, (config.BROADCAST_IP, config.PORT))
    for data in recv(s, config.DISCOVER_TIMEOUT, async):
        if data:
            msg, (addr, port) = data
            if config.STATICHOSTS and\
                    "%s:%i" % (addr, port) in config.STATICHOSTS:
                continue
            if msg.startswith(hello):
                name = msg[len(hello)+1:]
                url = "http://%s:%i/" % (resolve(addr), port)
                yield (name, url)
            else:
                yield None
        else: