0

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?

Abhinav
  • 992
  • 2
  • 11
  • 26

2 Answers2

1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • But will this help me to append the new results? – Abhinav Aug 01 '12 at 09:00
  • 1
    @sHoM The returned value of `sniff` isn't actually a list, but a custom object that implements list-like access (with the `__getitem__` function). That's the reason the type is `instance`. – Some programmer dude Aug 01 '12 at 09:16
  • We all love Python for its readability. Let's be obvious, let's be verbatim, let's use `copy` when we're copying stuff. –  Aug 01 '12 at 14:25
1

Use standard library copy to copy data structures in Python: http://docs.python.org/library/copy.html