【问题标题】:Python Classes and boundsPython 类和边界
【发布时间】:2021-07-10 05:21:32
【问题描述】:

试图围绕我为 Scapy 找到的一段代码展开思考。

from scapy.utils import RawPcapReader
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, TCP
        for pkt_data, pkt_metadata in RawPcapReader(file_name):

        ether_pkt = Ether(pkt_data)          
        if 'type' not in ether_pkt.fields:
            # LLC frames will have 'len' instead of 'type'.
            # We disregard those
            continue

        if ether_pkt.type != 0x0800:
            # disregard non-IPv4 packets
            continue

        ip_pkt = ether_pkt[IP]

让我困惑的部分是我的对象 ether_pkt 被分配到类 Ether 但是随着 ip_pkt = ether_pkt[IP]

的变化

这里发生了什么?

【问题讨论】:

    标签: python-3.x python-class


    【解决方案1】:

    python 的一个有趣的事情是你可以bind all operators to do custom things。例如,您可以创建一个对象,其中 + 运算符执行完全不同的操作。

    在 scapy 中,括号运算符被实现为从数据包中“获取下一层”。在这里,您通过指定第一层来剖析数据包:以太网。这将剖析也剖析所有子层,其中IP。

    pkt = Ether()/IP()
    pkt[IP] # Only the IP layer
    d = bytes(pkt)  # The full packet as bytes
    Ether(d)[IP]  # Dissect the packet, get the IP layer
    

    更多信息请关注https://scapy.readthedocs.io/en/latest/usage.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 2013-05-31
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多