python - scapy:如何从 fields_desc 获取 ByteEnumField 值?

标签 python scapy

我正在使用 scapy 的 bgp 层,我想知道是否可以获得 scapy Fields 类中定义的一些常量值。

例如,类 BGPPathAttr 包含一个字段 type_code,它引用一个包含 NEXT_HOP 和中定义的魔数(Magic Number) 3 之间关联的映射。 RFC 4271。我想通过创建类似于 BGPPathAttr.type_code.NEXT_HOP

的语句来访问值 3

谢谢

scapy 代码的摘录 ./scapy/scapy/contrib/bgp.py

path_attributes = {
    0: "Reserved",
    1: "ORIGIN",  # RFC 4271
    2: "AS_PATH",  # RFC 4271
    3: "NEXT_HOP",  # RFC 4271
    [...]
}

class BGPPathAttr(Packet):

   name = "BGPPathAttr"
   fields_desc = [
       ByteEnumField("type_code", 0, path_attributes)
   ]

最佳答案

你好,

您可以使用 s2i 属性(以及 i2s 进行反向翻译):

>>> BGPPathAttr.type_code.s2i['NEXT_HOP']
3
>>> BGPPathAttr.type_code.i2s[3]
'NEXT_HOP'

关于python - scapy:如何从 fields_desc 获取 ByteEnumField 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54691571/

相关文章:

python - 如何在 DataFrame 中添加行,在 python 中的每个 for 循环之后迭代更新它,最好是在 pandas 中

使用 numpy 中的加载文本导入 cdv 时出现 ' b ' 的 Python 问题

python - 带过滤器或减法或类似的计数器

python - 如何对函数内部进行的函数调用序列进行单元测试 [python]

python - 如何将过滤后的 pyshark FileCapture 保存到新的 pcap 文件?

python scapy : how to translate port numbers to service names?

python - 如何从命令行导入然后迭代导入的模块?

Python scapy : pcap file read, 操作写入pcap

python-2.7 - Python : Can't import a function from another. py 文件

haskell - Haskell 中是否有成熟的用于网络数据包处理的库?