本文整理匯總了Python中scapy.fields.BitField方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.BitField方法的具體用法?Python fields.BitField怎麽用?Python fields.BitField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scapy.fields
的用法示例。
在下文中一共展示了fields.BitField方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getfield
# 需要導入模塊: from scapy import fields [as 別名]
# 或者: from scapy.fields import BitField [as 別名]
def getfield(self, pkt, s):
# type: (Optional[packet.Packet], Union[str, Tuple[str, int]]) -> Tuple[Union[Tuple[str, int], str], int] # noqa: E501
"""
:param packet.Packet|None pkt: the packet instance containing this field instance; probably unused. # noqa: E501
:param str|(str, int) s: either a str if size%8==0 or a tuple with the string to parse from and the number of # noqa: E501
bits already consumed by previous bitfield-compatible fields.
:return: (str|(str, int), int): Returns the remaining string and the parsed value. May return a tuple if there # noqa: E501
are remaining bits to parse in the first byte. Returned value is equal to default value # noqa: E501
:raises: AssertionError
"""
r = super(HPackMagicBitField, self).getfield(pkt, s)
assert (
isinstance(r, tuple) and
len(r) == 2 and
isinstance(r[1], six.integer_types)
), 'Second element of BitField.getfield return value expected to be an int or a long; API change detected' # noqa: E501
assert r[1] == self._magic, 'Invalid value parsed from s; error in class guessing detected!' # noqa: E501
return r
示例2: addfield
# 需要導入模塊: from scapy import fields [as 別名]
# 或者: from scapy.fields import BitField [as 別名]
def addfield(self, pkt, s, val):
# type: (Optional[packet.Packet], Union[str, Tuple[str, int, int]], int) -> str # noqa: E501
"""
An AbstractUVarIntField prefix always consumes the remaining bits
of a BitField;if no current BitField is in use (no tuple in
entry) then the prefix length is 8 bits and the whole byte is to
be consumed
:param packet.Packet|None pkt: the packet containing this field.
Probably unused.
:param str|(str, int, long) s: the string to append this field to.
A tuple indicates that some bits were already generated by another
bitfield-compatible field. This MUST be the case if "size" is not 8.
The int is the number of bits already generated in the first byte of
the str. The long is the value that was generated by the previous
bitfield-compatible fields.
:param int val: the positive or null value to be added.
:return: str: s concatenated with the machine representation of this
field.
:raises: AssertionError
"""
assert(val >= 0)
if isinstance(s, bytes):
assert self.size == 8, 'EINVAL: s: tuple expected when prefix_len is not a full byte' # noqa: E501
return s + self.i2m(pkt, val)
# s is a tuple
# assert(s[1] >= 0)
# assert(s[2] >= 0)
# assert (8 - s[1]) == self.size, 'EINVAL: s: not enough bits remaining in current byte to read the prefix' # noqa: E501
if val >= self._max_value:
return s[0] + chb((s[2] << self.size) + self._max_value) + self.i2m(pkt, val)[1:] # noqa: E501
# This AbstractUVarIntField is only one byte long; setting the prefix value # noqa: E501
# and appending the resulting byte to the string
return s[0] + chb((s[2] << self.size) + orb(self.i2m(pkt, val)))