List cannot be copied
-
24-06-2021 - |
Question
I am using the sniff() function of Scapy to sniff packet from the ethernet. I store it in a variable called pkt. Later I want to make a copy of the same by appending contents of pkt in another variable buffpkt. Initially I declared both the variables as list but once pkt store the sniff() output it changes its type to instance. Below is the code.
pkt=[]
buffpkt=[]
pkt=sniff(prn=lambda x:x.sprintf("{IP : %IP.src% %IP.dst%\n\n} "),timeout=5,store=1)
buffpkt=pkt
I want to make a big list of all the packets sniffed by appending the results to buffpkt but I cannot. Any suggestions?
Solution
By just assigning one list to another, you just make the first list point to the second list. To copy the list you can use the slicing operator, like this:
buffpkt = pkt[:]
Now buffpkt
will be a copy.
OTHER TIPS
Use standard library copy
to copy data structures in Python: http://docs.python.org/library/copy.html