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


Python Frame.body方法代码示例

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


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

示例1: receiver

# 需要导入模块: from ws4py.framing import Frame [as 别名]
# 或者: from ws4py.framing.Frame import body [as 别名]
    def receiver(self):
        """
        Parser that keeps trying to interpret bytes it is fed with as
        incoming frames part of a message.

        Control message are single frames only while data messages, like text
        and binary, may be fragmented accross frames.

        The way it works is by instanciating a :class:`wspy.framing.Frame` object,
        then running its parser generator which yields how much bytes
        it requires to performs its task. The stream parser yields this value
        to its caller and feeds the frame parser.

        When the frame parser raises :exc:`StopIteration`, the stream parser
        tries to make sense of the parsed frame. It dispatches the frame's bytes
        to the most appropriate message type based on the frame's opcode.

        Overall this makes the stream parser totally agonstic to
        the data provider.
        """
        utf8validator = Utf8Validator()
        running = True
        frame = None
        while running:
            frame = Frame()
            while 1:
                try:
                    bytes = (yield next(frame.parser))
                    frame.parser.send(bytes)
                except StopIteration:
                    frame._cleanup()
                    bytes = frame.body

                    # Let's avoid unmasking when there is no payload
                    if bytes:
                        if frame.masking_key and self.expect_masking:
                            bytes = frame.unmask(bytes)
                        elif not frame.masking_key and self.expect_masking:
                            msg = CloseControlMessage(code=1002, reason='Missing masking when expected')
                            self.errors.append(msg)
                            break
                        elif frame.masking_key and not self.expect_masking:
                            msg = CloseControlMessage(code=1002, reason='Masked when not expected')
                            self.errors.append(msg)
                            break
                        else:
                            bytes = bytearray(bytes)
                        
                    if frame.opcode == OPCODE_TEXT:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(code=1002, reason='Received a new message before completing previous')
                            self.errors.append(msg)
                            break

                        m = TextMessage(bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                        if bytes:
                            is_valid, end_on_code_point, _, _ = utf8validator.validate(bytes)

                            if not is_valid or (m.completed and not end_on_code_point):
                                self.errors.append(CloseControlMessage(code=1007, reason='Invalid UTF-8 bytes'))
                                break

                    elif frame.opcode == OPCODE_BINARY:
                        m = BinaryMessage(bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                    elif frame.opcode == OPCODE_CONTINUATION:
                        m = self.message
                        if m is None:
                            self.errors.append(CloseControlMessage(code=1002, reason='Message not started yet'))
                            break
                        
                        m.extend(bytes)
                        m.completed = (frame.fin == 1)
                        if m.opcode == OPCODE_TEXT:
                            if bytes:
                                is_valid, end_on_code_point, _, _ = utf8validator.validate(bytes)
                                
                                if not is_valid or (m.completed and not end_on_code_point):
                                    self.errors.append(CloseControlMessage(code=1007, reason='Invalid UTF-8 bytes'))
                                    break

                    elif frame.opcode == OPCODE_CLOSE:
                        code = 1000
                        reason = ""
                        if frame.payload_length == 0:
                            self.closing = CloseControlMessage(code=1000)
                        elif frame.payload_length == 1:
                            self.closing = CloseControlMessage(code=1002, reason='Payload has invalid length')
                        else:
                            try:
                                code = int(unpack("!H", enc(bytes[0:2]))[0])
                            except TypeError:
                                code = 1002
                                reason = 'Invalid Closing Frame Code Type'
#.........这里部分代码省略.........
开发者ID:peeyush113,项目名称:WebSocket-for-Python,代码行数:103,代码来源:streaming.py

示例2: receiver

# 需要导入模块: from ws4py.framing import Frame [as 别名]
# 或者: from ws4py.framing.Frame import body [as 别名]
    def receiver(self):
        global logAudio
        """
        Parser that keeps trying to interpret bytes it is fed with as
        incoming frames part of a message.

        Control message are single frames only while data messages, like text
        and binary, may be fragmented accross frames.

        The way it works is by instanciating a :class:`wspy.framing.Frame` object,
        then running its parser generator which yields how much bytes
        it requires to performs its task. The stream parser yields this value
        to its caller and feeds the frame parser.

        When the frame parser raises :exc:`StopIteration`, the stream parser
        tries to make sense of the parsed frame. It dispatches the frame's bytes
        to the most appropriate message type based on the frame's opcode.

        Overall this makes the stream parser totally agonstic to
        the data provider.
        """

        logAudio=settings.getVal("logAudio")
        print("debug: streaming.py in receiver function | logAudio=%s" % logAudio)

        utf8validator = Utf8Validator()
        running = True
        frame = None
        while running:
            frame = Frame()
            while 1:
                try:
                    some_bytes = (yield next(frame.parser))
                    frame.parser.send(some_bytes)
                except GeneratorExit:
                    running = False
                    break
                except StopIteration:
                    frame._cleanup()
                    some_bytes = frame.body

                    # Let's avoid unmasking when there is no payload
                    if some_bytes:
                        if frame.masking_key and self.expect_masking:
                            some_bytes = frame.unmask(some_bytes)
                        elif not frame.masking_key and self.expect_masking:
                            msg = CloseControlMessage(code=1002, reason='Missing masking when expected')
                            self.errors.append(msg)
                            break
                        elif frame.masking_key and not self.expect_masking:
                            msg = CloseControlMessage(code=1002, reason='Masked when not expected')
                            self.errors.append(msg)
                            break
                        else:
                            # If we reach this stage, it's because
                            # the frame wasn't masked and we didn't expect
                            # it anyway. Therefore, on py2k, the bytes
                            # are actually a str object and can't be used
                            # in the utf8 validator as we need integers
                            # when we get each byte one by one.
                            # Our only solution here is to convert our
                            # string to a bytearray.
                            some_bytes = bytearray(some_bytes)

                    if frame.opcode == OPCODE_TEXT:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(code=1002, reason='Received a new message before completing previous')
                            self.errors.append(msg)
                            break

                        m = TextMessage(some_bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                        if some_bytes:
                            is_valid, end_on_code_point, _, _ = utf8validator.validate(some_bytes)

                            if not is_valid or (m.completed and not end_on_code_point):
                                self.errors.append(CloseControlMessage(code=1007, reason='Invalid UTF-8 bytes'))
                                break

                    elif frame.opcode == OPCODE_BINARY:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(code=1002, reason='Received a new message before completing previous')
                            self.errors.append(msg)
                            break

                        if logAudio != "0":
                           print("debug:: received a binary frame with %d bytes, log it via localhost listner (port:%s)" % (len(some_bytes),logAudio))
                           #bstr1=""
                           #for i in range(20):
                           #    str1="%d " % some_bytes[i]
                           #    bstr1 = bstr1 + " " + str1
                           #print("binary msg 1st 20 bytes: %s" % bstr1)

                           # Create a TCP/IP socket
                           sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#.........这里部分代码省略.........
开发者ID:harryCM,项目名称:hello-world,代码行数:103,代码来源:streaming.py


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