tcp_transport.py 文件源码

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

项目:BitBot 作者: crack00r 项目源码 文件源码
def receive(self, **kwargs):
        """Receives a TCP message (tuple(sequence number, body)) from the
           connected peer.

           If a named 'timeout' parameter is present, it will override
           'self.timeout', and this can be a 'timedelta' or 'None'.
         """
        timeout = kwargs.get('timeout', self.timeout)

        # First read everything we need
        packet_length_bytes = self.tcp_client.read(4, timeout)
        packet_length = int.from_bytes(packet_length_bytes, byteorder='little')

        seq_bytes = self.tcp_client.read(4, timeout)
        seq = int.from_bytes(seq_bytes, byteorder='little')

        body = self.tcp_client.read(packet_length - 12, timeout)

        checksum = int.from_bytes(
            self.tcp_client.read(4, timeout), byteorder='little', signed=False)

        # Then perform the checks
        rv = packet_length_bytes + seq_bytes + body
        valid_checksum = crc32(rv)

        if checksum != valid_checksum:
            raise InvalidChecksumError(checksum, valid_checksum)

        # If we passed the tests, we can then return a valid TCP message
        return seq, body
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号