Scapy设置类属性问题
我正在使用 scapy,并且有一个与 scapy 和 python 相关的问题。我尝试使用变量作为 scapy 字段名称。这是一段代码摘录,应该说明我尝试做的事情(它不起作用):
class Test(Packet):
name = "Test23"
def __init__(self,foo=None):
self.foo=foo
fields_desc = [
BitField(self.foo, 0x0, 4),
BitField("bar", 0x3, 4)
]
现在我想问题是由于 fields_desc 是类属性而不是类变量这一事实造成的。现在,我怎样才能达到我想要的呢? (在运行时/类初始化时设置 self.foo 的名称?)
如果有任何帮助,我将不胜感激。 致以最诚挚的问候
编辑:附加一个计数器就足够了。我尝试过:
class Counter:
count = 0
def __init__(self):
self.__class__.count += 1
foo = [
"lala"+str(count)
]
print foo
a=Counter()
a.count
print a.foo
b=Counter()
b.count
print b.foo
但似乎不起作用。如果你能给我指出正确的方向那就太好了,不知何故我迷路了。
I'm working with scapy and have a question related to scapy but also python. I try to use variables as scapy field-names. Here is an extract of code that should illustrate what I try to do (It is not working):
class Test(Packet):
name = "Test23"
def __init__(self,foo=None):
self.foo=foo
fields_desc = [
BitField(self.foo, 0x0, 4),
BitField("bar", 0x3, 4)
]
Now I imagine the problem is due to the fact that fields_desc is a class attribute and not a class variable. Now, how could I reach what I want? (setting the name of self.foo at runtime/class inizialisation?)
I would be thankfull for any help.
With best regards
Edit: Appending an counter would be enough. I tried:
class Counter:
count = 0
def __init__(self):
self.__class__.count += 1
foo = [
"lala"+str(count)
]
print foo
a=Counter()
a.count
print a.foo
b=Counter()
b.count
print b.foo
But doesn't seem to work. Would be cool if you could point me to the correct direction, somehow I'm lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为类属性是在类构造函数运行之前初始化的,因此在创建 fields_desc 时尚未分配 self.foo 。尝试将 fields_desc 声明为空数组,并在 __init__ 函数内将 BitField 对象附加到其中。
我不知道您继承的 Packet 类是如何工作的,但您可能还想查看 super() 函数。
编辑:也许您正在寻找类似的东西?
在此处输出
有关类变量的更多信息
I think class attributes are initialized before the class constructor runs, so self.foo has not been assigned when fields_desc is created. Try declaring fields_desc as an empty array and appending the BitField objects to it inside your
__init__
function.I don't know how the Packet class you're inheriting from works, but you may also want to look at the super() function.
Edit: Maybe you're looking for something like this?
Output
More info on class variables here
我猜您想要做的是根据您使用“getfield”解析的原始数据输入更改第一个 BitField 的名称?这有点很难说……
无论如何,从函数
pre_dissect
中,您可以查看原始数据并根据需要将 Fields 附加到 self.fields_desc 中。使用 pre_dissect 时,请确保在函数末尾返回未经修改的原始数据包数据。根据我的经验,这很少是正确的方法,从长远来看,通常 ConditionalField 和类重载效果更好。
祝你好运
I'm guessing what you are trying to do is change the name of the first BitField based on the raw data input you're parsing with 'getfield'? It's kinda hard to tell...
Anyway, from the function
pre_dissect
you can look at the raw data and append Fields to the self.fields_desc as you wish. When using pre_dissect make sure to return the raw packet data at the end of the function un-modified.This in my experience is very rarely the right way to go on this, usually ConditionalField's and class-overloading works much better in the long run.
Good luck