I kno开发者_StackOverflow中文版w questions like this have been asked plenty of times before, but I think this is subtley different.
I am attempting to write a flexible traffic generator in Python using scapy. Producing the packet is fine, but when it comes to sending traffic at a sufficiently fast rate (for my needs, somewhere in the range of 500-700 packets per second), I seem to have hit a wall at around 20-30 pps.
I believe that there may be some need for threading, or am I missing something easier?
On my system I get much better performance sending ethernet frames with sendp compared to sending IP packets using send.
# this gives appox 500pps on my system
pe=Ether()/IP(dst="10.13.37.218")/ICMP()
sendp(pe, loop=True)
# this gives approx 100pps on my system
pi=IP(dst="10.13.37.218")/ICMP()
send(pi, loop=True)
But sending (precreated) packet on the socket manually is way faster:
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
s.bind(("eth0", 0))
pe=Ether()/IP(dst="10.13.37.218")/ICMP()
data = pe.build()
while True:
s.send(data)
But moving the pe.build() into the loop drastically reduces speed, hinting that it is the actual packet building that takes time.
FTR, while the above answer is correct, it can also be implemented at level 2 using a Scapy socket:
from scapy.all import *
sock = conf.L2socket()
pe=Ether()/IP(dst="10.13.37.218")/ICMP()
data = pe.build()
while True:
pe.send(data)
Though if sending packets in loop is your objective:
send(Ether()/IP(dst="10.13.37.218")/ICMP(), loop=1)
Will do :-)
精彩评论