parameters: (self)
return_statement: return True
docstring: Parse a WebSocket frame. If there is not a complete frame in the
          buffer, return without modifying the buffer.
docstring_summary: Parse a WebSocket frame. If there is not a complete frame in the
          buffer, return without modifying the buffer.
docstring_tokens: Parse a WebSocket frame . If there is not a complete frame in the buffer return without modifying the buffer .
function: def parse_frame(self):
          """
          Parse a WebSocket frame. If there is not a complete frame in the
          buffer, return without modifying the buffer.
          """
          buf = self.buffer
          payload_start = 2
  
          # try to pull first two bytes
          if len(buf) < 3:
              return
          b = ord(buf[0])
          fin = b & 0x80      # 1st bit
          # next 3 bits reserved
          opcode = b & 0x0f   # low 4 bits
          b2 = ord(buf[1])
          mask = b2 & 0x80    # high bit of the second byte
          length = b2 & 0x7f    # low 7 bits of the second byte
  
          # check that enough bytes remain
          if len(buf) < payload_start + 4:
              return
          elif length == 126:
              length, = struct.unpack(">H", buf[2:4])
              payload_start += 2
          elif length == 127:
              length, = struct.unpack(">I", buf[2:6])
              payload_start += 4
  
          if mask:
              mask_bytes = [ord(b) for b in buf[payload_start:payload_start + 4]]
              payload_start += 4
  
          # is there a complete frame in the buffer?
          if len(buf) < payload_start + length:
              return
  
          # remove leading bytes, decode if necessary, dispatch
          payload = buf[payload_start:payload_start + length]
          self.buffer = buf[payload_start + length:]
  
          # use xor and mask bytes to unmask data
          if mask:
              unmasked = [mask_bytes[i % 4] ^ ord(b)
                              for b, i in zip(payload, range(len(payload)))]
              payload = "".join([chr(c) for c in unmasked])
  
          if opcode == WebSocketConnection.TEXT:
              s = payload.decode("UTF8")
              self.handler.dispatch(s)
          if opcode == WebSocketConnection.BINARY:
              self.handler.dispatch(payload)
          return True
function_tokens: def parse_frame ( self ) : buf = self . buffer payload_start = 2 # try to pull first two bytes if len ( buf ) < 3 : return b = ord ( buf [ 0 ] ) fin = b & 0x80 # 1st bit # next 3 bits reserved opcode = b & 0x0f # low 4 bits b2 = ord ( buf [ 1 ] ) mask = b2 & 0x80 # high bit of the second byte length = b2 & 0x7f # low 7 bits of the second byte # check that enough bytes remain if len ( buf ) < payload_start + 4 : return elif length == 126 : length , = struct . unpack ( ">H" , buf [ 2 : 4 ] ) payload_start += 2 elif length == 127 : length , = struct . unpack ( ">I" , buf [ 2 : 6 ] ) payload_start += 4 if mask : mask_bytes = [ ord ( b ) for b in buf [ payload_start : payload_start + 4 ] ] payload_start += 4 # is there a complete frame in the buffer? if len ( buf ) < payload_start + length : return # remove leading bytes, decode if necessary, dispatch payload = buf [ payload_start : payload_start + length ] self . buffer = buf [ payload_start + length : ] # use xor and mask bytes to unmask data if mask : unmasked = [ mask_bytes [ i % 4 ] ^ ord ( b ) for b , i in zip ( payload , range ( len ( payload ) ) ) ] payload = "" . join ( [ chr ( c ) for c in unmasked ] ) if opcode == WebSocketConnection . TEXT : s = payload . decode ( "UTF8" ) self . handler . dispatch ( s ) if opcode == WebSocketConnection . BINARY : self . handler . dispatch ( payload ) return True