为什么在python中使用套接字接收重复的帧?

时间:2014-03-06 09:45:24

标签: python sockets frame

我创建了一个简单的客户端/服务器代码来使用套接字发送/接收帧,但是在接收处,我收到了像这样的重复数据包

> num seq= 4751 num seq= 4751 num seq= 4752 num seq= 4752 num seq= 4753
> num seq= 4753 num seq= 4754 num seq= 4754 num seq= 4755 num seq= 4755
> num seq= 4756 num seq= 4756 num seq= 4757 num seq= 4757 num seq= 4758
> num seq= 4758 num seq= 4759 num seq= 4759 num seq= 4760 num seq= 4760
> num seq= 4761 num seq= 4761 num seq= 4762 num seq= 4762 num seq= 4763
> num seq= 4763 num seq= 4764 num seq= 4764 num seq= 4765 num seq= 4765
> num seq= 4766 num seq= 4766 num seq= 4767 num seq= 4767 num seq= 4768
> num seq= 4768 num seq= 4769 num seq= 4769 num seq= 4770 num seq= 4770
> num seq= 4771 num seq= 4771 num seq= 4772 num seq= 4772 num seq= 4773
> num seq= 4773 num seq= 4774 num seq= 4774 num seq= 4775 num seq= 4775
> num seq= 4776 num seq= 4776 num seq= 4777 num seq= 4777 num seq= 4778

我无法理解为什么数据包在接收处重复!! ??

这是客户端代码:

from socket import *
import binascii

#creation of the RAW socket
s=socket(AF_PACKET, SOCK_RAW)

# bind the socket to the interface
s.bind(("eth0",0))

# Ethernet header
src="\x01\x01\x01\x01\x01\x01"
dst="\x02\x02\x02\x02\x02\x02"
ethertype="74"  #type or length

# generate an infinite traffic 

i=0       # frame number

while True :
        payload=("0"*(100-len(str(i))))+str(i)
        cr=binascii.crc32(dst+src+ethertype+payload)
        res=''
        for j in range(4): # calculate the crc
            t=cr & 0xFF
            cr >>= 8
            res='%02X%s' % (t, res)
        s.send(dst+src+ethertype+payload+res)    # send the frame
        i+=1

服务器代码是:

from scapy.all import *

def traiter(p) :

    k=str(p) #transform the frame to a string

    print "num seq=",int(k[14:112]) # print the sequence number

def main():

# intercept packet and send it to traiter

    sniff(iface="lo", prn=lambda x: traiter(x))

if __name__ == "__main__":
   sys.exit(main())

1 个答案:

答案 0 :(得分:0)

这是一种正常行为:如果源和目标位于同一台机器上,当您在环回接口上嗅探时,您会捕获退出和输入数据包!因此数据包实际上并不重复。