How to collect TCP packets in Python?

How to collect TCP packets in Python? Are there any existing tools for this?

Thank!: -)

+5
source share
2 answers

To re-build TCP, you need to use something like pynids http://jon.oberheide.org/pynids/ .

You can also create your own using pylibpcap, dpkt or scapy.

Reassembling TCP is very difficult with lots of edges. I would not recommend doing this yourself if you need a reliable solution.

+12
source

... TCP , . , , IP-, RAW-, IP. :

import socket

# the public network interface
HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))

# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

# receive a package
print s.recvfrom(65565)

# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

python: http://docs.python.org/library/socket.html

+4

All Articles