本文整理汇总了Python中six.byte2int函数的典型用法代码示例。如果您正苦于以下问题:Python byte2int函数的具体用法?Python byte2int怎么用?Python byte2int使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了byte2int函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode
def decode(self, encoded_packet):
"""Decode a transmitted package."""
b64 = False
self.packet_type = six.byte2int(encoded_packet[0:1])
if self.packet_type == 98: # 'b' --> binary base64 encoded packet
self.binary = True
encoded_packet = encoded_packet[1:]
self.packet_type = six.byte2int(encoded_packet[0:1])
self.packet_type -= 48
b64 = True
elif self.packet_type >= 48:
self.packet_type -= 48
self.binary = False
else:
self.binary = True
self.data = None
if len(encoded_packet) > 1:
if self.binary:
if b64:
self.data = base64.b64decode(encoded_packet[1:])
else:
self.data = encoded_packet[1:]
else:
try:
self.data = self.json.loads(
encoded_packet[1:].decode('utf-8'))
except ValueError:
self.data = encoded_packet[1:].decode('utf-8')
示例2: decode
def decode(self, encoded_payload):
"""Decode a transmitted payload."""
self.packets = []
while encoded_payload:
if six.byte2int(encoded_payload[0:1]) <= 1:
packet_len = 0
i = 1
while six.byte2int(encoded_payload[i:i + 1]) != 255:
packet_len = packet_len * 10 + six.byte2int(
encoded_payload[i:i + 1])
i += 1
self.packets.append(packet.Packet(
encoded_packet=encoded_payload[i + 1:i + 1 + packet_len]))
else:
i = encoded_payload.find(b':')
if i == -1:
raise ValueError('invalid payload')
# extracting the packet out of the payload is extremely
# inefficient, because the payload needs to be treated as
# binary, but the non-binary packets have to be parsed as
# unicode. Luckily this complication only applies to long
# polling, as the websocket transport sends packets
# individually wrapped.
packet_len = int(encoded_payload[0:i])
pkt = encoded_payload.decode('utf-8', errors='ignore')[
i + 1: i + 1 + packet_len].encode('utf-8')
self.packets.append(packet.Packet(encoded_packet=pkt))
# the engine.io protocol sends the packet length in
# utf-8 characters, but we need it in bytes to be able to
# jump to the next packet in the payload
packet_len = len(pkt)
encoded_payload = encoded_payload[i + 1 + packet_len:]
示例3: decode_version
def decode_version(version):
""" Takes a byte version and decodes it into human-readable <major>.<minor> format. """
if len(version) != 1:
raise ValueError("Can only decode a single byte!")
major = six.byte2int(version) >> 4
minor = six.byte2int(version) & 15
return ('%d.%d' % (major, minor)).encode('ascii')
示例4: validate
def validate(self, skip_utf8_validation=False):
"""
validate the ABNF frame.
skip_utf8_validation: skip utf8 validation.
"""
if self.rsv1 or self.rsv2 or self.rsv3:
raise WebSocketProtocolException("rsv is not implemented, yet")
if self.opcode not in ABNF.OPCODES:
raise WebSocketProtocolException("Invalid opcode %r", self.opcode)
if self.opcode == ABNF.OPCODE_PING and not self.fin:
raise WebSocketProtocolException("Invalid ping frame.")
if self.opcode == ABNF.OPCODE_CLOSE:
l = len(self.data)
if not l:
return
if l == 1 or l >= 126:
raise WebSocketProtocolException("Invalid close frame.")
if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]):
raise WebSocketProtocolException("Invalid close frame.")
code = 256 * six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2])
if not self._is_valid_close_status(code):
raise WebSocketProtocolException("Invalid close opcode.")
示例5: _wait_close
def _wait_close(self):
logger.debug('hxsocks _wait_close')
self.settimeout(8)
while 1:
try:
ctlen = self._rfile_read(2)
if not ctlen:
raise IOError(0, '')
ctlen = struct.unpack('>H', self.pskcipher.decrypt(ctlen))[0]
ct = self._rfile_read(ctlen)
mac = self._rfile_read(MAC_LEN)
data = self.cipher.decrypt(ct, mac)
pad_len = byte2int(data)
if 0 < pad_len < 8:
# fake chunk, drop
if pad_len == 1:
self.send_fake_chunk(2)
# server should be sending another chunk right away
continue
data = data[1:0-pad_len] if byte2int(data) else data[1:]
if not data:
logger.debug('hxsocks add to pool')
self.pooled = 1
POOL.put(self.hxsServer.parse.hostname, self, self.hxsServer.name)
self.readable = 0
break
except Exception:
self._sock.close()
return
示例6: test_array_infinite_nested_block
def test_array_infinite_nested_block():
random.seed(0)
class leaf(pint.uint32_t): pass
class rootcontainer(parray.block):
_object_ = leaf
class acontainer(rootcontainer):
blocksize = lambda x: 8
class bcontainer(rootcontainer):
_object_ = pint.uint16_t
blocksize = lambda x: 8
class ccontainer(rootcontainer):
_object_ = pint.uint8_t
blocksize = lambda x: 8
class arr(parray.infinite):
def randomcontainer(self):
l = [ acontainer, bcontainer, ccontainer ]
return random.sample(l, 1)[0]
_object_ = randomcontainer
string = str().join([ six.int2byte(random.randint(six.byte2int('A'),six.byte2int('Z'))) for x in six.moves.range(0x100) ])
a = arr(source=provider.string(string))
a=a.l
if a.blocksize() == 0x108:
raise Success
示例7: __getvalue__
def __getvalue__(self):
if not self.initializedQ():
raise error.InitializationError(self, 'int')
if self.byteorder is config.byteorder.bigendian:
return six.moves.reduce(lambda x,y: x << 8 | six.byte2int(y), self.serialize(), 0)
elif self.byteorder is config.byteorder.littleendian:
return six.moves.reduce(lambda x,y: x << 8 | six.byte2int(y), reversed(self.serialize()), 0)
raise error.SyntaxError(self, 'integer_t.int', message='Unknown integer endianness {!r}'.format(self.byteorder))
示例8: _decode_carddata
def _decode_carddata(data, card_type, reftime = None):
"""Decodes a data record read from an SI Card."""
ret = {}
card = SIReader.CARD[card_type]
# the slicing of data is necessary for Python 3 to get a bytes object instead
# of an int
ret['card_number'] = SIReader._decode_cardnr(b'\x00'
+ data[card['CN2']:card['CN2']+1]
+ data[card['CN1']:card['CN1']+1]
+ data[card['CN0']:card['CN0']+1])
ret['start'] = SIReader._decode_time(data[card['ST']:card['ST']+2],
data[card['STD']] if card['STD'] else None,
reftime)
ret['finish'] = SIReader._decode_time(data[card['FT']:card['FT']+2],
data[card['FTD']] if card['FTD'] is not None else None,
reftime)
ret['check'] = SIReader._decode_time(data[card['CT']:card['CT']+2],
data[card['CTD']] if card['CTD'] is not None else None,
reftime)
if card['LT'] is not None:
ret['clear'] = SIReader._decode_time(data[card['LT']:card['LT']+2],
data[card['LTD']] if card['LTD'] is not None else None,
reftime)
else:
ret['clear'] = None # SI 5 and 9 cards don't store the clear time
punch_count = byte2int(data[card['RC']:card['RC']+1])
if card_type == 'SI5':
# RC is the index of the next punch on SI5
punch_count -= 1
if punch_count > card['PM']:
punch_count = card['PM']
ret['punches'] = []
p = 0
i = card['P1']
while p < punch_count:
if card_type == 'SI5' and i % 16 == 0:
# first byte of each block is reserved for punches 31-36
i += 1
ptd = data[i + card['PTD']] if card['PTD'] is not None else None
cn = byte2int(data[i + card['CN']])
pt = data[i + card['PTH']:i + card['PTL']+1]
SIReader._append_punch(ret['punches'], cn, pt, ptd, reftime)
i += card['PL']
p += 1
return ret
示例9: _get_close_args
def _get_close_args(self,data):
""" this functions extracts the code, reason from the close body
if they exists, and if the self.on_close except three arguments """
import inspect
# if the on_close callback is "old", just return empty list
if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3:
return []
if data and len(data) >=2:
code = 256*six.byte2int(data[0]) + six.byte2int(data[1])
reason = data[2:].decode('utf-8')
return [code,reason]
return [None,None]
示例10: _read_command
def _read_command(self, timeout = None):
try:
if timeout != None:
old_timeout = self._serial.timeout
self._serial.timeout = timeout
char = self._serial.read()
if timeout != None:
self._serial.timeout = old_timeout
if char == b'':
raise SIReaderTimeout('No data available')
elif char == SIReader.NAK:
raise SIReaderException('Invalid command or parameter.')
elif char != SIReader.STX:
self._serial.flushInput()
raise SIReaderException('Invalid start byte %s' % hex(byte2int(char)))
# Read command, length, data, crc, ETX
cmd = self._serial.read()
length = self._serial.read()
station = self._serial.read(2)
self.station_code = SIReader._to_int(station)
data = self._serial.read(byte2int(length)-2)
crc = self._serial.read(2)
etx = self._serial.read()
if self._debug:
print("<<== command '%s', len %i, station %s, data %s, crc %s, etx %s" % (hexlify(cmd).decode('ascii'),
byte2int(length),
hexlify(station).decode('ascii'),
' '.join([hexlify(int2byte(c)).decode('ascii') for c in data]),
hexlify(crc).decode('ascii'),
hexlify(etx).decode('ascii'),
))
if etx != SIReader.ETX:
raise SIReaderException('No ETX byte received.')
if not SIReader._crc_check(cmd + length + station + data, crc):
raise SIReaderException('CRC check failed')
if self._logfile:
self._logfile.write('r %s %s\n' % (datetime.now(), char + cmd + length + station + data + crc + etx))
self._logfile.flush()
os.fsync(self._logfile)
except (SerialException, OSError) as msg:
raise SIReaderException('Error reading command: %s' % msg)
return (cmd, data)
示例11: derive
def derive(self, type, site, counter=1):
value = ""
seed = self.seed(site, counter)
try:
templates = Templates[type].value
except KeyError as e:
log.error("Unknown key type '{}'".format(type))
raise e
template = templates[six.byte2int(seed[0]) % len(templates)]
for i in range(0, len(template)):
passChars = CHARACTER_CLASSES[template[i]]
passChar = passChars[six.byte2int(seed[i + 1]) % len(passChars)]
value += passChar
return value
示例12: poll_punch
def poll_punch(self, timeout=0):
"""Polls for new punches.
@return: list of (cardnr, punchtime) tuples, empty list if no new punches are available
"""
if not self.proto_config['ext_proto']:
raise SIReaderException('This command only supports stations in "Extended Protocol" '
'mode. Switch mode first')
if not self.proto_config['auto_send']:
raise SIReaderException('This command only supports stations in "Autosend" '
'mode. Switch mode first')
punches = []
while True:
try:
c = self._read_command(timeout = timeout)
except SIReaderTimeout:
break
if c[0] == SIReader.C_TRANS_REC:
cur_offset = SIReader._to_int(c[1][SIReader.T_OFFSET:SIReader.T_OFFSET+3])
if self._next_offset is not None:
while self._next_offset < cur_offset:
# recover lost punches
punches.append(self._read_punch(self._next_offset))
self._next_offset += SIReader.REC_LEN
self._next_offset = cur_offset + SIReader.REC_LEN
punches.append( (self._decode_cardnr(c[1][SIReader.T_CN:SIReader.T_CN+4]),
self._decode_time(c[1][SIReader.T_TIME:SIReader.T_TIME+2])) )
else:
raise SIReaderException('Unexpected command %s received' % hex(byte2int(c[0])))
return punches
示例13: decode_name
def decode_name(name):
# ToDo: Rewrite this simpler, we're using less than written
"""
Perform first and second level decoding of name as specified in RFC 1001 (Section 4)
:param string name: the name to dencode
:return string: the decoded name.
"""
name_length = ord(name[0])
assert name_length == 32
decoded_name = re.sub('..', _do_first_level_decoding, name[1:33])
if name[33] == '\0':
return 34, decoded_name, ''
else:
decoded_domain = ''
offset = 34
while 1:
domain_length = byte2int(name[offset:offset+1])
if domain_length == 0:
break
decoded_domain = '.' + name[offset:offset + domain_length]
offset += domain_length
return offset + 1, decoded_name, decoded_domain
示例14: extract_message
def extract_message(cls, raw_bytes):
if len(raw_bytes) < 2:
return None, raw_bytes
if six.byte2int(raw_bytes) != 123:
raise FramingError(
'Broken state. Expected JSON Object, got: %s' % raw_bytes)
stack = [123]
uniesc = 0
poppers = {91: [93], 123: [125], 34: [34]}
adders = {91: [34, 91, 123], 123: [34, 91, 123], 34: [92], 92: [117]}
for idx in range(1, len(raw_bytes)):
cbyte = six.indexbytes(raw_bytes, idx)
if cbyte in poppers.get(stack[-1], []):
stack.pop()
elif cbyte in adders.get(stack[-1], []):
stack.append(cbyte)
elif stack[-1] == 92:
stack.pop()
elif stack[-1] == 117:
uniesc += 1
if uniesc >= 4:
stack = stack[:-2]
uniesc = 0
if not stack:
return raw_bytes[:idx + 1], raw_bytes[idx + 1:]
return None, raw_bytes
示例15: decode
def decode(self, input, errors='strict'):
"""
Decode byte array to string
:param input: byte array to convert to unicode string
:param errors: defines the error handling to apply
:return: returns a tuple (output object, length consumed)
"""
decode_buffer = u""
consumed = 0
num = 0
for value in input:
consumed += 1
num |= byte2int([value])
if num == self._ESCAPE:
num <<= 8
continue
try:
decode_buffer += unichr(self._decode_map[num])
except KeyError as ex:
if errors == 'replace':
decode_buffer += u'\ufffd'
elif errors == 'ignore':
pass
else:
if num & (self._ESCAPE << 8):
raise ValueError("'%s' codec can't decode byte 0x%x in position %d" %
(self.NAME, ex.args[0] & 0xff, consumed - 1))
else:
raise ValueError("'%s' codec can't decode byte 0x%x in position %d" %
(self.NAME, ex.args[0], consumed - 1))
num = 0
return decode_buffer, consumed