Giter Club home page Giter Club logo

pypcap's Introduction

pypcap

Python/C bindings for the libpcap library. Most of the functions are 1:1 mapped to the libpcap library.

Example 1

>>> import pypcap
>>> pypcap.pcap_lookupdev()
'fw0'
>>> pypcap.pcap_findalldevs()
['fw0', 'en0', 'utun0', 'en1', 'p2p0']
>>> pypcap.pcap_lookupnet('en1')
('192.168.1.10', '255.255.252.0')
>>> pypcap.pcap_lib_version()
'libpcap version 1.1.1'

Example 2

from pypcap import pcap
import dpkt
x = pcap()
x.pcap_create( 'eth0' )
x.pcap_set_promisc( True )
x.pcap_activate()
x.pcap_compile( 'udp' )

def pcap_callback(pkt):
    # do stuff with packet
    eth = dpkt.ethernet.Ethernet(pkt)

x.pcap_set_callback(pcap_callback)
x.pcap_loop()

Notes

Linux

  • On 64-bit Linux, the buffer's size should be least the snap length set for the handle in use. If not, you will end up with a "can't mmap rx ring: Invalid argument" error.
  • On Linux, root privileges are required even for module functions (pcap_lookupdev, etc )

Research

  • Designing an iterator on top of pcap_next() is easy, but the fact that it calls pcap_dispatch() with cnt=1 seems like a performance drag compared to pcap_loop/callback. Needs more investigation.
  • Read more about GIL implementaion and thread-state for C Extensions

Remaining Work

  • Make it thread-safe
  • GIL / thread-state
  • Reference counting
  • Add pcap_dump routines
  • Add iterators for pcap_next
  • Add unittests
  • Add timestamp to pcap_loop callback
  • WIN32 Support

License

See LICENSE file

Requires

  • Python 2.5 or later
  • python-dev
  • libpcap-dev

Tested on

  • OSX 10.8 - x86_64
  • Linux - x86_64, armv6l
  • Not tested on 32bit Linux/OSX, but should work

pypcap's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.