当前位置: 首页>>代码示例>>Python>>正文


Python Stream.start_receiving方法代码示例

本文整理汇总了Python中stream.Stream.start_receiving方法的典型用法代码示例。如果您正苦于以下问题:Python Stream.start_receiving方法的具体用法?Python Stream.start_receiving怎么用?Python Stream.start_receiving使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stream.Stream的用法示例。


在下文中一共展示了Stream.start_receiving方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Tunnel

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import start_receiving [as 别名]

#.........这里部分代码省略.........
        self._disable_heartbeat()
        self._stream.close()

    def initialize(self):
        if self._stream is None:
            self._stream = Stream(prefix='TUNNEL')

        # self._stream.set_buffer_size(BUFF_SIZE)
        self._stream.set_tcp_no_delay()

        self._stream.append_send_handler(obscure.pack_data)
        self._stream.append_send_handler(obscure.random_padding)
        # self._stream.append_send_handler(obscure.gen_aes_encrypt())
        self._stream.append_send_handler(obscure.gen_xor_encrypt())
        # self._stream.append_send_handler(obscure.base64_encode)
        self._stream.append_send_handler(obscure.gen_http_encode(self._connect_to is not None))
        self._stream.append_receive_handler(obscure.gen_http_decode(self._connect_to is not None))
        # self._stream.append_receive_handler(obscure.base64_decode)
        self._stream.append_receive_handler(obscure.gen_xor_decrypt())
        # self._stream.append_receive_handler(obscure.gen_aes_decrypt())
        self._stream.append_receive_handler(obscure.unpad_random)
        self._stream.append_receive_handler(obscure.unpack_data)

        self._stream.set_on_ready_to_send(lambda _: self._on_tunnel_ready_to_send())
        self._stream.set_on_send_buffer_full(lambda _: self._on_tunnel_send_buffer_full())
        self._stream.set_on_received(lambda _, data, addr: self._on_received(data, addr))
        self._stream.set_on_fin_received(lambda _: self._on_fin_received())
        self._stream.set_on_closed(lambda _: self._on_closed())

        if self._connect_to is not None:
            self._stream.connect(*self._connect_to)
        else:
            self._stream.set_on_decode_error(lambda _, received: self._on_decode_error(received))
            self._stream.start_receiving()
        self._enable_heartbeat()

    def register(self, key, conn):
        _logger.debug('%s, register: %s(%s)', str(self), str(key), str(conn))
        assert(key not in self.connections)
        self.connections[key] = conn

    def deregister(self, key):
        _logger.debug('%s, deregister(%s)', str(self), str(key))
        if key in self.connections:
            del self.connections[key]

    def get_connection(self, key):
        if key in self.connections:
            return self.connections[key]
        else:
            _logger.debug('no such connection: %s', str(key))
            return None

    def clear_connections(self):
        _logger.debug('%s, clear_connections (%d)', str(self), len(self.connections))
        self.connections.clear()

    def is_ready_to_send(self):
        return self._stream.is_ready_to_send()

    def _send_content(self, type_, id_, content):
        if id_ is None:
            to_send = struct.pack('!HI', type_, 0) + '\x00' * 16
        else:
            to_send = struct.pack('!HI', type_, len(content)) + id_.get_bytes() + content
        self._stream.send(to_send)
开发者ID:binasc,项目名称:utils,代码行数:70,代码来源:tunnel.py


注:本文中的stream.Stream.start_receiving方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。