예제 #1
0
 def handle_msg(self):
     msg = read_msg(self.stream)
     command = msg['command'].decode()
     print(f'Received a "{command}"')
     method_name = f'handle_{command}'
     if hasattr(self, method_name):
         getattr(self, method_name)(msg['payload'])
 def handle_msg(self):
     msg = read_msg(self.stream)
     command = msg['command'].decode()
     logger.info('Received a "{}"'.format(command))
     method_name = "handle_{}".format(command)
     if hasattr(self, method_name):
         getattr(self, method_name)(msg['payload'])
    def handle_msg(self):
        msg = read_msg(self.stream)
        command = msg['command']
        payload_len = len(msg['payload'])
        print('Received a {} containing {} bytes'.format(command, payload_len))

        # respond to ping
        if command == b'ping':
            res = serialize_msg(command=b'pong', payload=msg['payload'])
            self.sock.sendall(res)
            print('Sent pong')

        # handle peer lists
        if command == b'addr':
            payload = read_addr_payload(BytesIO(msg['payload']))
            if len(payload['addresses']) > 1:
                self.nodes_discovered.extend(
                    [Node(a['ip'], a['port']) for a in payload['addresses']])
예제 #4
0
    def handle_msg(self):
        msg = read_msg(self.stream)
        command = msg['command']
        payload_len = len(msg['payload'])
        print(f'Received a "{command}" containing {payload_len} bytes')

        # Respond to "ping"
        if command == b'ping':
            res = serialize_msg(command=b'pong', payload=msg['payload'])
            self.sock.sendall(res)
            print("Send 'pong'")

        # Specially handle peer lists
        if command == b'addr':
            payload = read_addr_payload(BytesIO(msg['payload']))
            if len(payload['addresses']) > 1:
                self.nodes_discovered = [
                    Node(a['ip'], a['port']) for a in payload['addresses']
                ]
예제 #5
0
def crawler(addresses):
    while True:
        # Get next address from addresses and connect
        address = addresses.pop()

        try:
            # Establish connection
            print(f'Connecting to {address}')
            sock = handshake(address)  # FIXME: save the version payload
            stream = sock.makefile('rb')

            # Request peer's peers
            sock.sendall(serialize_msg(b'getaddr'))

            # Print every gossip message we receive
            while True:
                msg = read_msg(stream)
                command = msg['command']
                payload_len = len(msg['payload'])
                print(f'Received a "{command}" containing {payload_len} bytes')

                # Respond to "ping"
                if command == b'ping':
                    res = serialize_msg(command=b'pong',
                                        payload=msg['payload'])
                    sock.sendall(res)
                    print("Send 'pong'")

                # Specially handle peer lists
                if command == b'addr':
                    payload = read_addr_payload(BytesIO(msg['payload']))
                    if len(payload['addresses']) > 1:
                        addresses.extend([(a['ip'], a['port'])
                                          for a in payload['addresses']])
                        break
        except Exception as e:
            print(f'Got error: {str(e)}')
            continue