Skip to main content

Library to read/write the pcap-ng format used by various packet sniffers.

Project description

Python library to parse the pcap-ng format used by newer versions of dumpcap & similar tools (wireshark, winpcap, …).

Documentation

If you prefer the RTD theme, or want documentation for any version other than the latest, head here:

http://python-pcapng.readthedocs.org/en/latest/

If you prefer the more comfortable, page-wide, default sphinx theme, a documentation mirror is hosted on GitHub pages:

http://rshk.github.io/python-pcapng/

CI build status

Branch

Status

master

https://travis-ci.org/rshk/python-pcapng.svg?branch=master

develop

https://travis-ci.org/rshk/python-pcapng.svg?branch=develop

Source code

Source, issue tracker etc. on GitHub: https://github.com/rshk/python-pcapng

Get the source from git:

git clone https://github.com/rshk/python-pcapng

Download zip of the latest version:

https://github.com/rshk/python-pcapng/archive/master.zip

Install from pypi:

pip install python-pcapng

PyPI status

The official page on the Python Package Index is: https://pypi-hypernode.com/pypi/python-pcapng

Latest PyPI version Number of PyPI downloads Supported Python versions Development Status License

Why this library?

  • I need to decently extract some information from a bunch of pcap-ng files, but apparently tcpdump has some problems reading those files,

    I couldn’t find other nice tools nor Python bindings to a library able to parse this format, so..

  • In general, it appears there are (quite a bunch of!) Python modules to parse the old (much simpler) format, but nothing for the new one.

  • And, they usually completely lack any form of documentation.

Isn’t it slow?

Yes, I guess it would be much slower than something written in C, but I’m much better at Python than C.

..and I need to get things done, and CPU time is not that expensive :)

(Maybe I’ll give a try porting the thing to Cython to speed it up, but anyways, pure-Python libraries are always useful, eg. for PyPy).

How do I use it?

Basic usage is as simple as:

from pcapng import FileScanner

with open('/tmp/mycapture.pcap', 'rb') as fp:
    scanner = FileScanner(fp)
    for block in scanner:
        pass  # do something with the block...

Have a look at the blocks documentation to see what they do; also, the examples directory contains some example scripts using the library.

Hacking

Format specification is here:

https://github.com/pcapng/pcapng/

Contributions are welcome, please contact me if you’re planning to do some big change, so that we can sort out the best way to integrate it.

Or even better, open an issue so the whole world can participate in the discussion :)

Pcap-ng write support

Write support exists as of version 2.0.0. See the file examples/generate_pcapng.py for an example of the minimum code needed to generate a pcapng file.

In most cases, this library will prevent you from creating broken data. If you want to create marginal pcapng files, e.g. as test cases for other software, you can do that by adjusting the “strictness” of the library, as in:

from pcapng.strictness import Strictness, set_strictness
set_strictness(Strictness.FIX)

Recognized values are Strictness.FORBID (the default), Strictness.FIX (warn about problems, fix if possible), Strictness.WARN (warn only), and Strictness.NONE (no warnings). Circumstances that will result in strictness warnings include:

  • Adding multiples of a non-repeatable option to a block

  • Adding a SPB to a file with more than one interface

  • Writing a PB (PBs are obsolete and not to be used in new files)

  • Writing EPB/SPB/PB/ISB before writing any IDBs

Creating a release

  1. Create a tag for the new version:

    git tag v2.0.0 -m 'Version 2.0.0'
  2. Install build dependencies in a virtualenv:

    python -m venv ./.build-venv
    ./.build-venv/bin/python -m pip install build twine
  3. Build source and wheel distributions:

    rm -rf ./dist *.egg-info
    ./.build-venv/bin/python -m build
  4. Use Twine to upload to pypi:

    twine upload dist/*

Troubleshooting

If you get some crazy version number like 2.0.1.dev0+g7bd8575.d20220310 instead of what you expect (eg 2.0.0), it’s because you have uncommitted or untracked files in your local working copy, or you created more commits after creating the tag. Such a version number will be refused by pypi (and it’s not a good version number anyways), so make sure you have a clean working copy before building.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-pcapng-2.1.0.tar.gz (82.1 kB view details)

Uploaded Source

Built Distribution

python_pcapng-2.1.0-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file python-pcapng-2.1.0.tar.gz.

File metadata

  • Download URL: python-pcapng-2.1.0.tar.gz
  • Upload date:
  • Size: 82.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.4

File hashes

Hashes for python-pcapng-2.1.0.tar.gz
Algorithm Hash digest
SHA256 7bea4c2973006293a0576b4e5fdb5af7f42035f2db2684b48b832e9dbb21707f
MD5 5ab97d7bf95492b91d50959917ed45c7
BLAKE2b-256 b8e60c8a8d32e1b9d348d05abef4cb74f3c9e09563ce8f7f73614d6b55ea2b48

See more details on using hashes here.

File details

Details for the file python_pcapng-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_pcapng-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.4

File hashes

Hashes for python_pcapng-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be89c635beac116d7f0f61a41532ed06e8e5e58e357a25550004f11e81f052bf
MD5 bba5fdf85fb75e4ed835af1b86822a35
BLAKE2b-256 5ae618b1d571bfb64852772635a9d2b6ba880ed2e464ed1c419a4d014cb3523f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page