本文整理汇总了Python中scapy.dadict.DADict方法的典型用法代码示例。如果您正苦于以下问题:Python dadict.DADict方法的具体用法?Python dadict.DADict怎么用?Python dadict.DADict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scapy.dadict
的用法示例。
在下文中一共展示了dadict.DADict方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from scapy import dadict [as 别名]
# 或者: from scapy.dadict import DADict [as 别名]
def __init__(self, name, default, enum, fmt="H"):
""" Initializes enum fields.
@param name: name of this field
@param default: default value of this field
@param enum: either a dict or a tuple of two callables. Dict keys are # noqa: E501
the internal values, while the dict values are the
user-friendly representations. If the tuple is provided, # noqa: E501
the first callable receives the internal value as
parameter and returns the user-friendly representation
and the second callable does the converse. The first
callable may return None to default to a literal string
(repr()) representation.
@param fmt: struct.pack format used to parse and serialize the
internal value from and to machine representation.
"""
if isinstance(enum, ObservableDict):
enum.observe(self)
if isinstance(enum, tuple):
self.i2s_cb = enum[0]
self.s2i_cb = enum[1]
self.i2s = None
self.s2i = None
else:
i2s = self.i2s = {}
s2i = self.s2i = {}
self.i2s_cb = None
self.s2i_cb = None
if isinstance(enum, list):
keys = list(range(len(enum)))
elif isinstance(enum, DADict):
keys = enum.keys()
else:
keys = list(enum)
if any(isinstance(x, str) for x in keys):
i2s, s2i = s2i, i2s
for k in keys:
i2s[k] = enum[k]
s2i[enum[k]] = k
Field.__init__(self, name, default, fmt)
示例2: load_protocols
# 需要导入模块: from scapy import dadict [as 别名]
# 或者: from scapy.dadict import DADict [as 别名]
def load_protocols(filename, _fallback=None, _integer_base=10, _cls=DADict):
""""Parse /etc/protocols and return values as a dictionary."""
spaces = re.compile(b"[ \t]+|\n")
dct = _cls(_name=filename)
def _process_data(fdesc):
for line in fdesc:
try:
shrp = line.find(b"#")
if shrp >= 0:
line = line[:shrp]
line = line.strip()
if not line:
continue
lt = tuple(re.split(spaces, line))
if len(lt) < 2 or not lt[0]:
continue
dct[int(lt[1], _integer_base)] = fixname(lt[0])
except Exception as e:
log_loading.info(
"Couldn't parse file [%s]: line [%r] (%s)",
filename,
line,
e,
)
try:
if not filename:
raise IOError
with open(filename, "rb") as fdesc:
_process_data(fdesc)
except IOError:
if _fallback:
_process_data(_fallback.split(b"\n"))
else:
log_loading.info("Can't open %s file", filename)
return dct
示例3: load_services
# 需要导入模块: from scapy import dadict [as 别名]
# 或者: from scapy.dadict import DADict [as 别名]
def load_services(filename):
spaces = re.compile(b"[ \t]+|\n")
tdct = DADict(_name="%s-tcp" % filename)
udct = DADict(_name="%s-udp" % filename)
try:
with open(filename, "rb") as fdesc:
for line in fdesc:
try:
shrp = line.find(b"#")
if shrp >= 0:
line = line[:shrp]
line = line.strip()
if not line:
continue
lt = tuple(re.split(spaces, line))
if len(lt) < 2 or not lt[0]:
continue
dtct = None
if lt[1].endswith(b"/tcp"):
dtct = tdct
elif lt[1].endswith(b"/udp"):
dtct = udct
else:
continue
port = lt[1].split(b'/')[0]
name = fixname(lt[0])
if b"-" in port:
sport, eport = port.split(b"-")
for i in range(int(sport), int(eport) + 1):
dtct[i] = name
else:
dtct[int(port)] = name
except Exception as e:
log_loading.warning(
"Couldn't parse file [%s]: line [%r] (%s)",
filename,
line,
e,
)
except IOError:
log_loading.info("Can't open /etc/services file")
return tdct, udct