在带有scapy库的函数中调用函数不起作用。但如果不在函数中工作

2024-10-01 15:36:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为Practice创建一个第二层协议。有我的导入(在两个文件中):

from pystack.layers.ethernet import EthernetProtocol
from pystack.layers.arp import ARPProtocol
from pystack.layers.ethernet import Ether
from scapytrame import EtherData
from scapy.all import *

当我在文件中处理这些行时,它工作得很好(发件人.py)在

^{pr2}$

所以我收到了这个包,我可以在WireShark上看到它 但现在我想用一个函数来做,但这行不通。还有第二个文件:

def sender(frames, macSrc="00:21:70:a2:b7:6d", iface="eth0") :
    i = 0
    sentBack = False
    while(i < len(frames)):

        # Sending packet
        frames[i].seq_ack = hex(i%2);
        frames[i].show()
        Scapy.sendp(frames[i])

        # Acknowledge + Next packet
        macDst = frames[i].dst
        filtre = "ether src " + macDst
        ack = sniff(iface=str(iface), filter=str(filtre), count=1, timeout=5)
        if not ack:
            sentBack = True
            continue
        ackData = EtherData(str(ack[0]))
        if int(ackData.seq_ack) == 2:
            sentBack = False
            i = i + 1
        else:
            continue

def receiver(macSrc="00:21:70:a2:b7:6d", iface="eth0") :
    nextFrame = 0
    while(True):
        ack = sniff(iface=str(iface), filter=str(filtre), count=1)
        ackData = EtherData(str(ack[0]))
        if int(ackData.seq_ack) == nextFrame%2:
            ackData.show()
            sender([EtherData(src="00:21:70:a2:b7:6d", dst=ackData.dst, seq_ack=2)])
            nextFrame = nextFrame + 1

frames_test = [ EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
            EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
            EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
            EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
            EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9)]

sender(frames_test)

然后我得到一个错误:

Traceback (most recent call last):
  File "ProtAckPosAndTrans.py", line 47, in <module>
    sender(frames_test)
  File "ProtAckPosAndTrans.py", line 15, in sender
    sendp(frames[i])
  File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 259, in sendp
    __gen_send(conf.L2socket(iface=iface, *args, **kargs), x, inter=inter, loop=loop, count=count, verbose=verbose, realtime=realtime)
  File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 234, in __gen_send
    s.send(p)
  File "/usr/lib/python2.7/dist-packages/scapy/supersocket.py", line 32, in send
    sx = str(x)
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 261, in __str__
    return self.build()
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 319, in build
    p = self.do_build()
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 308, in do_build
    pkt = self.self_build()
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 299, in self_build
    p = f.addfield(self, p, val)
  File "/usr/lib/python2.7/dist-packages/scapy/fields.py", line 70, in addfield
    return s+struct.pack(self.fmt, self.i2m(pkt,val))
struct.error: cannot convert argument to integer

frames[i].show()工作得很好,我还做了一个'print frames[i].'所有的东西都找到了。。。所以我不明白为什么它不起作用。在

我到处找都找不到。感谢您的回答:)


Tags: inpya2framesusrlineseqscapy

热门问题