本文整理汇总了Python中bitstring.ConstBitStream.read方法的典型用法代码示例。如果您正苦于以下问题:Python ConstBitStream.read方法的具体用法?Python ConstBitStream.read怎么用?Python ConstBitStream.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bitstring.ConstBitStream
的用法示例。
在下文中一共展示了ConstBitStream.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_bytes
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def from_bytes(cls, bitstream):
packet = cls()
# Convert to ConstBitStream (if not already provided)
if not isinstance(bitstream, ConstBitStream):
if isinstance(bitstream, Bits):
bitstream = ConstBitStream(auto=bitstream)
else:
bitstream = ConstBitStream(bytes=bitstream)
# Read the next header type
packet.next_header = bitstream.read('uint:8')
# Read the header length, given in multiples of 8 octets
header_length = bitstream.read('uint:8') + 1
# Read the options
options_length = (header_length * 8) - 2
packet.options = bitstream.read('bytes:%d' % options_length)
# And the rest is payload
remaining = bitstream[bitstream.pos:]
packet.payload = remaining.bytes
payload_class = protocol_registry.get_type_class(packet.next_header)
if payload_class:
packet.payload = payload_class.from_bytes(packet.payload)
# Verify that the properties make sense
packet.sanitize()
return packet
示例2: testFromFile
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def testFromFile(self):
s = CBS(filename='test.m1v')
self.assertEqual(s[0:32].hex, '000001b3')
self.assertEqual(s.read(8 * 4).hex, '000001b3')
width = s.read(12).uint
height = s.read(12).uint
self.assertEqual((width, height), (352, 288))
示例3: mc_parser
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def mc_parser():
out, buff = yield
while True:
header_bytes = yield from buff.read(24)
header = ConstBitStream(header_bytes)
readlist_fmt = 'uint:8, uint:8, uint:16'
magic, opcode, key_length = header.readlist(readlist_fmt)
extras_length, data_type, status = header.readlist(readlist_fmt)
total_body_length = header.read('uint:32')
opaque = header.read('uint:32')
cas = header.read('uint:64')
extras = None
if extras_length:
extras = yield from buff.read(extras_length)
key = None
if key_length:
key = yield from buff.read(key_length)
value_length = total_body_length - (key_length + extras_length)
value = None
if value_length:
value = yield from buff.read(value_length)
out.feed_data(MCResponse(opcode, data_type, status, cas,
extras, key, value))
示例4: huff_decode
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def huff_decode(enc_file):
enc = ConstBitStream(enc_file)
tree = []
tree_sz = enc.read("uint:8")
while tree_sz > 0:
s = enc.read("bytes:1")
c_sz = enc.read("uint:8")
c = BitArray(enc.read("bits:"+str(c_sz)))
tree.append([s, c])
tree_sz -= 1
tree = sorted(tree, key = lambda v: v[1].len)
text = ""
while True:
try:
found = False
for s,c in tree:
if enc.startswith(c, enc.pos):
print enc[enc.pos:].bin
code = enc.read("bits:"+str(c.len))
text += s
found = True
break
if found == False:
raise ReadError
except ReadError:
break
return text
示例5: __init__
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def __init__(self, src, *args, **kwargs):
super(EC3SpecificBox,self).__init__(src, *args,**kwargs)
data = src.read(self.size)
bs = ConstBitStream(bytes=data)
self.data_rate = bs.read('uint:13')
self.num_ind_sub = 1+bs.read('uint:3')
self.substreams = []
for i in range(self.num_ind_sub):
self.substreams.append(EC3SpecificBox.SubStream(bs))
示例6: testRead
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def testRead(self):
s = CBS("0b100011110001")
a = s.read("pad:1")
self.assertEqual(a, None)
self.assertEqual(s.pos, 1)
a = s.read(3)
self.assertEqual(a, CBS("0b000"))
a = s.read("pad:0")
self.assertEqual(a, None)
self.assertEqual(s.pos, 4)
示例7: from_bytes
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def from_bytes(cls, bitstream):
'''
Parse the given packet and update properties accordingly
'''
packet = cls()
# Convert to ConstBitStream (if not already provided)
if not isinstance(bitstream, ConstBitStream):
if isinstance(bitstream, Bits):
bitstream = ConstBitStream(auto=bitstream)
else:
bitstream = ConstBitStream(bytes=bitstream)
# Read the type
type_nr = bitstream.read('uint:4')
if type_nr != packet.message_type:
msg = 'Invalid bitstream for a {0} packet'
class_name = packet.__class__.__name__
raise ValueError(msg.format(class_name))
# Read if this is a reply
packet.is_reply = bitstream.read('bool')
# Skip reserved bits
packet._reserved1 = bitstream.read(27)
# Read the nonce
packet.nonce = bitstream.read('bytes:8')
# Read the key id
packet.key_id = bitstream.read('uint:16')
# Read the authentication data
data_length = bitstream.read('uint:16')
packet.authentication_data = bitstream.read('bytes:%d' % data_length)
# Read the TTL
packet.ttl = bitstream.read('uint:32')
# Skip reserved bits
packet._reserved2 = bitstream.read(8)
# Store the EID prefix mask length until we need it
eid_prefix_len = bitstream.read('uint:8')
# Read the EID prefix
packet.eid_prefix = read_afi_address_from_bitstream(bitstream, eid_prefix_len)
# Read the reply
packet.reply = read_afi_address_from_bitstream(bitstream)
# Verify that the properties make sense
packet.sanitize()
return packet
示例8: from_bytes
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def from_bytes(cls, bitstream):
'''
Parse the given packet and update properties accordingly
'''
packet = cls()
# Convert to ConstBitStream (if not already provided)
if not isinstance(bitstream, ConstBitStream):
if isinstance(bitstream, Bits):
bitstream = ConstBitStream(auto=bitstream)
else:
bitstream = ConstBitStream(bytes=bitstream)
# Read the type
type_nr = bitstream.read('uint:4')
if type_nr != packet.message_type:
msg = 'Invalid bitstream for a {0} packet'
class_name = packet.__class__.__name__
raise ValueError(msg.format(class_name))
# Read the flags
has_xtr_site_id = bitstream.read('bool')
# Skip reserved bits
packet._reserved1 = bitstream.read(19)
# Store the record count until we need it
record_count = bitstream.read('uint:8')
# Read the nonce
packet.nonce = bitstream.read('bytes:8')
# Read the key id
packet.key_id = bitstream.read('uint:16')
# Read the authentication data
data_length = bitstream.read('uint:16')
packet.authentication_data = bitstream.read('bytes:%d' % data_length)
# Read the records
for dummy in range(record_count):
record = MapRegisterRecord.from_bytes(bitstream)
packet.records.append(record)
# Read the xtr-id and site-id
if has_xtr_site_id:
packet.xtr_id = bitstream.read('uint:128')
packet.site_id = bitstream.read('uint:64')
# Verify that the properties make sense
packet.sanitize()
return packet
示例9: _parse_binary_feed
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def _parse_binary_feed(self, feed):
binaryfeed = bytearray(b64decode(feed))
bitstream = ConstBitStream(binaryfeed)
payload_encoding = binaryfeed[0]
if payload_encoding != bitstream.read("uint:8"):
raise PyiCloudBinaryFeedParseError("Missmatch betweeen binaryfeed and bistream payload encoding")
ASSET_PAYLOAD = 255
ASSET_WITH_ORIENTATION_PAYLOAD = 254
ASPECT_RATIOS = [
0.75,
4.0 / 3.0 - 3.0 * (4.0 / 3.0 - 1.0) / 4.0,
4.0 / 3.0 - 2.0 * (4.0 / 3.0 - 1.0) / 4.0,
1.25,
4.0 / 3.0,
1.5 - 2.0 * (1.5 - 4.0 / 3.0) / 3.0,
1.5 - 1.0 * (1.5 - 4.0 / 3.0) / 3.0,
1.5,
1.5694444444444444,
1.6388888888888888,
1.7083333333333333,
16.0 / 9.0,
2.0 - 2.0 * (2.0 - 16.0 / 9.0) / 3.0,
2.0 - 1.0 * (2.0 - 16.0 / 9.0) / 3.0,
2,
3,
]
valid_payloads = [ASSET_PAYLOAD, ASSET_WITH_ORIENTATION_PAYLOAD]
if payload_encoding not in valid_payloads:
raise PyiCloudBinaryFeedParseError("Unknown payload encoding '%s'" % payload_encoding)
assets = {}
while len(bitstream) - bitstream.pos >= 48:
range_start = bitstream.read("uint:24")
range_length = bitstream.read("uint:24")
range_end = range_start + range_length
previous_asset_id = 0
for index in range(range_start, range_end):
aspect_ratio = ASPECT_RATIOS[bitstream.read("uint:4")]
id_size = bitstream.read("uint:2")
if id_size:
# A size has been reserved for the asset id
asset_id = bitstream.read("uint:%s" % (2 + 8 * id_size))
else:
# The id is just an increment to a previous id
asset_id = previous_asset_id + bitstream.read("uint:2") + 1
orientation = None
if payload_encoding == ASSET_WITH_ORIENTATION_PAYLOAD:
orientation = bitstream.read("uint:3")
assets[index] = PhotoAsset(index, asset_id, aspect_ratio, orientation, self)
previous_asset_id = asset_id
return assets.values()
示例10: from_bytes
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def from_bytes(cls, bitstream):
'''
Parse the given packet and update properties accordingly
'''
packet = cls()
# Convert to ConstBitStream (if not already provided)
if not isinstance(bitstream, ConstBitStream):
if isinstance(bitstream, Bits):
bitstream = ConstBitStream(auto=bitstream)
else:
bitstream = ConstBitStream(bytes=bitstream)
# Read the type
type_nr = bitstream.read('uint:4')
if type_nr != packet.message_type:
msg = 'Invalid bitstream for a {0} packet'
class_name = packet.__class__.__name__
raise ValueError(msg.format(class_name))
# Read the flags
(packet.probe,
packet.enlra_enabled,
packet.security) = bitstream.readlist('3*bool')
# Skip reserved bits
packet._reserved1 = bitstream.read(17)
# Store the record count until we need it
record_count = bitstream.read('uint:8')
# Read the nonce
packet.nonce = bitstream.read('bytes:8')
# Read the records
for dummy in range(record_count):
record = MapReplyRecord.from_bytes(bitstream)
packet.records.append(record)
# If the security flag is set then there should be security data left
# TODO: deal with security flag [LISP-Security]
if packet.security:
raise NotImplementedError('Handling security data is not ' +
'implemented yet')
# Verify that the properties make sense
packet.sanitize()
return packet
示例11: from_bytes
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def from_bytes(cls, bitstream, decode_payload=True):
"""
Parse the given packet and update properties accordingly
"""
packet = cls()
# Convert to ConstBitStream (if not already provided)
if not isinstance(bitstream, ConstBitStream):
if isinstance(bitstream, Bits):
bitstream = ConstBitStream(auto=bitstream)
else:
bitstream = ConstBitStream(bytes=bitstream)
# Read the version
version = bitstream.read("uint:4")
if version != packet.version:
raise ValueError("Provided bytes do not contain an IPv6 packet")
# Read the traffic class
packet.traffic_class = bitstream.read("uint:8")
# Read the flow label
packet.flow_label = bitstream.read("uint:20")
# Read the payload length
payload_length = bitstream.read("uint:16")
# Read the next header type
packet.next_header = bitstream.read("uint:8")
# Read the hop limit
packet.hop_limit = bitstream.read("uint:8")
# Read the source and destination addresses
packet.source = IPv6Address(bitstream.read("uint:128"))
packet.destination = IPv6Address(bitstream.read("uint:128"))
# And the rest is payload
packet.payload = bitstream.read("bytes:%d" % payload_length)
if decode_payload:
payload_class = protocol_registry.get_type_class(packet.next_header)
if payload_class:
packet.payload = payload_class.from_bytes(packet.payload)
# There should be no remaining bits
if bitstream.pos != bitstream.len:
raise ValueError("Bits remaining after processing packet")
# Verify that the properties make sense
packet.sanitize()
return packet
示例12: testReading
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def testReading(self):
s = CBS(uie=333)
a = s.read('uie')
self.assertEqual(a, 333)
s = CBS('uie=12, sie=-9, sie=9, uie=1000000')
u = s.unpack('uie, 2*sie, uie')
self.assertEqual(u, [12, -9, 9, 1000000])
示例13: from_bytes
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def from_bytes(cls, bitstream):
'''
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| /| Priority | Weight | M Priority | M Weight |
| L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| o | Unused Flags |L|p|R| Loc-AFI |
| c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| \| Locator |
+-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
record = cls()
# Convert to ConstBitStream (if not already provided)
if not isinstance(bitstream, ConstBitStream):
if isinstance(bitstream, Bits):
bitstream = ConstBitStream(auto=bitstream)
else:
bitstream = ConstBitStream(bytes=bitstream)
# Read the priorities and weights
(record.priority, record.weight, record.m_priority,
record.m_weight) = bitstream.readlist('4*uint:8')
# Read over unused flags
record.reserved = bitstream.read(13)
# Read the flags
(record.local,
record.probed_locator,
record.reachable) = bitstream.readlist('3*bool')
# Read the locator
record.address = read_afi_address_from_bitstream(bitstream)
return record
示例14: encode
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def encode(self, infile, outfile):
s=''
chars=[]
print(BitArray(filename=infile).bin[2:])
f=ConstBitStream(filename=infile)
done=False
eof=False
while not done:
found=False
cursor=self.encoding
while not found:
try:
bit=f.read('uint:1')
except:
eof=True
bit=0
cursor=cursor[bit+1]
if len(cursor)==2: # leaf
found=True
val=cursor[1]
s=s+chr(val)
chars.append(val)
if eof:
done=True
f=open(outfile, 'wb')
f.write(s)
f.close()
print(chars)
print(BitArray(filename=outfile).bin[2:])
示例15: from_bytes
# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import read [as 别名]
def from_bytes(cls, bitstream):
'''
Parse the given packet and update properties accordingly
'''
packet = cls()
# Convert to ConstBitStream (if not already provided)
if not isinstance(bitstream, ConstBitStream):
if isinstance(bitstream, Bits):
bitstream = ConstBitStream(auto=bitstream)
else:
bitstream = ConstBitStream(bytes=bitstream)
# Read the source and destination ports
(packet.source_port,
packet.destination_port) = bitstream.readlist('2*uint:16')
# Store the length
length = bitstream.read('uint:16')
if length < 8:
raise ValueError('Invalid UDP length')
# Read the checksum
packet.checksum = bitstream.read('uint:16')
# And the rest is payload
payload_bytes = length - 8
packet.payload = bitstream.read('bytes:%d' % payload_bytes)
# LISP-specific handling
if packet.source_port == 4341 or packet.destination_port == 4341:
# Payload is a LISP data packet
from pylisp.packet.lisp.data import DataPacket
packet.payload = DataPacket.from_bytes(packet.payload)
elif packet.source_port == 4342 or packet.destination_port == 4342:
# Payload is a LISP control message
from pylisp.packet.lisp.control.base import ControlMessage
packet.payload = ControlMessage.from_bytes(packet.payload)
# There should be no remaining bits
if bitstream.pos != bitstream.len:
raise ValueError('Bits remaining after processing packet')
# Verify that the properties make sense
packet.sanitize()
return packet