Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mbudge opened this issue Nov 28, 2015 · 1 comment
Open

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

mbudge opened this issue Nov 28, 2015 · 1 comment

Comments

@mbudge
Copy link

mbudge commented Nov 28, 2015

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

@JonnoFTW
Copy link

Is it because you're not calling close? On your 3rd to last line you need

file.close()

Instead of file.close

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants