python - 将参数传递给 Scapy 的 Automaton.run() 方法
问题描述
我正在尝试使用 Scapy 的 Automaton 类创建一个自动机来解析不同的数据包。
为了做到这一点,我需要能够将数据包作为参数传递给自动机。一种方法是在创建自动机时传递数据包:
my_automaton = MyAutomaton(packet)
此参数将由parse_args
自动机代码中重载的函数处理:
class MyAutomaton(Automaton):
def parse_args(self, pkt, **kargs):
Automaton.parse_args(self, **kargs)
self.pkt = pkt
... REST OF CLASS ...
如果我为每个传入的数据包创建一个新的自动机,这会很好地工作。
但我只想创建一个自动机并使用不同的数据包运行它。就像是:
my_automaton = MyAutomaton()
my_automaton.run(pkt1)
my_automaton.run(pkt2)
根据文档,这应该是可能的(链接):
The parse_args() method is called with arguments given at __init__() and run(). Use that to parametrize the behaviour of your automaton.
通过在调用该方法时打印到控制台,parse_args
我验证了它确实在自动机创建和调用该run
方法时被调用。
但我似乎无法通过run
函数传递任何参数,我在这里错过了什么?
解决方案
如文档所示,您需要在初始化自动机时传递参数:
>>> TFTP_read("my_file", "192.168.1.128").run()
在你的情况下,那将是
my_automaton = MyAutomaton(pkt1)
my_automaton.run()
my_automaton2 = MyAutomaton(pkt2)
my_automaton2.run()
推荐阅读
- javascript - 如何在 JavaScript 字符串中使用/解析 HTML 实体和 Unicode 字符
- google-apps-script - 仅限数字 Google 表格自动备份
- java - akka 流是否提供有保证的交付
- reactjs - 使用 componentWillUnmount 时 Backhandler.removeeventlistener 无效
- mysql - 如何使用单个 mySql 查询从我的 1x3 矩阵层次结构中找到哪个孩子的孩子少于 3 个
- python - Python SQLAlchemy TypeError: unhashable type: 'dict' 尝试实例化具有一对多关系的模型时
- python - 将数据保存到 Mongo 数据库 - cpu 使用量太大
- jquery - jQuery 渲染为 pdf
- php - 无法在 AngularJS 中下载文件(PPTX)
- java - 在任何异常上终止 Spring Boot 任务