pynetwork / pypcap

pypcap - python libpcap module, forked from code.google.com/p/pypcap
Other
299 stars 75 forks source link

Stop pypcap from capturing and writing network traffic to a file #13

Closed mbudge closed 8 years ago

mbudge commented 8 years ago

I'm capturing network traffic with pypcap and writing it to a file with dpkt. Problem is I can't delete the file as I get an error saying it's still in use by another process.

seconds = 15
starttime = datetime.datetime.now()

pc = pcap.pcap()
file = open('file.pcap', 'wb')
pcapfile dpkt.pcap.Writer(file)

for timestamp, packet in pc:
    currenttime = datetime.datetime.now()
    timedelta = currenttime - starttime
    if timedelta.seconds > seconds:
        break

    pcapfile.writepkt(packet,timestamp)

file.close

time.sleep(10)

os.remove(file.pcap) = file in use by another process error

I'm currently overwriting the pcap file the next time I capture traffic. Doing this overwrites the file data most of the time, but occasionally it doesn't work and I end up with a large pcap file encompassing two sets of network capture data.

How do you close the pcapy/dpkt process handle?

Also does anyone know a better way to stop pcapy capturing traffic after 15 seconds?

Thanks

mbudge commented 8 years ago

Wrong project - https://github.com/dugsong/pypcap/issues

brifordwylie commented 8 years ago

@mbudge this is actually the right place... Dug Song's pypcap github repo is no longer active... I'll try to respond to your question on the dpkt project (as I see you've asked it there as well).