Skip to main content

netpolicy

Compiles deny and allow rules into an effective network policy. Written in Rust with PyO3.

deny and allow accept IP addresses, CIDR networks, and (ip, cidr) pairs. Pass a single string or pair, or an iterable of them. Any string may contain comma-separated networks, so a comma-separated feed and a list of rules go in one call: Policy(deny=[feed, *rules]). Allowed ranges are carved out of the denied ones. What remains is summarized into the smallest possible set of networks, in numeric order with IPv4 before IPv6, and joined with commas. Networks inside other denied networks, or next to them, merge into one.

from netpolicy import Policy

policy = Policy(
    deny=["1.2.3.4", "5.6.7.8", ("9.9.9.0", 24)],
    allow=[("9.9.9.5", 32)],
)
policy.render_excluding(["5.6.7.8", "10.0.0.0/24"])

Build Policy once and keep it for as long as its input stays the same. render_excluding() returns the comma-separated block list as ASCII bytes, with the additional allowed networks removed. If none of them overlap the policy, the result is unchanged. Call .decode() on it if you need a str.

Invalid deny entries

An invalid entry raises ValueError by default. When the deny list comes from an external feed, one bad entry shouldn't break the whole policy. Pass skip_invalid_deny=True to leave invalid deny entries out. They're listed in skipped with their error message:

policy = Policy(deny=feed, allow=allowed, skip_invalid_deny=True)
if policy.skipped:
    entry, error = policy.skipped[0]
    logger.warning("Skipped %d invalid deny entries, first: %s", len(policy.skipped), error)

Invalid allow entries always raise, because skipping one would block addresses that are meant to be allowed. The same goes for render_excluding(). Entries that are neither a string nor an (ip, cidr) pair raise TypeError in both modes, since that's a bug in the caller rather than bad data.

Keep an eye on the number of skipped entries. If the feed changes format, every entry fails and the policy comes out empty.

Development

You need Rust 1.85 or newer and uv.

uv sync --extra test          # creates .venv, builds the extension and installs pytest
uv run pytest tests           # parity tests against the ipaddress reference implementation
cargo test --lib              # Rust unit tests
cargo fmt                     # formats with the settings in rustfmt.toml
cargo clippy --all-targets    # lints, CI fails on any warning
uv run python bench/bench_policy.py   # timings on a production shaped data set

Metadata

Release files for netpolicy 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for netpolicy 0.3.0
File Size Uploaded Provenance Actions
netpolicy-0.3.0.tar.gz 24.1 kB With provenance Details for netpolicy-0.3.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for netpolicy 0.3.0
File Interpreter ABI Platform Actions
netpolicy-0.3.0-cp312-abi3-manylinux_2_28_x86_64.whl CPython 3.12 abi3 Linux glibc 2.28+ x86-64 Details for netpolicy-0.3.0-cp312-abi3-manylinux_2_28_x86_64.whl
netpolicy-0.3.0-cp312-abi3-manylinux_2_28_aarch64.whl CPython 3.12 abi3 Linux glibc 2.28+ ARM64 Details for netpolicy-0.3.0-cp312-abi3-manylinux_2_28_aarch64.whl
netpolicy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl CPython 3.12 abi3 macOS 11.0+ ARM64 Details for netpolicy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl

Total release size: 781.4 kB

Release files / netpolicy-0.3.0.tar.gz

Download URL netpolicy-0.3.0.tar.gz
Size 24.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4a5f9d9c00919edc15d9704e4b7faf9b798782f82c0afe20281073fad1f57839
BLAKE2b-256 checksum
How to use checksums
15fb5986a74c6675abd06b2b5e4e3e3dcf1b3c2c12b8bb1008eac3cf703ab13a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / netpolicy-0.3.0-cp312-abi3-manylinux_2_28_x86_64.whl

Download URL netpolicy-0.3.0-cp312-abi3-manylinux_2_28_x86_64.whl
Size 262.7 kB
Tags CPython 3.12 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
1aa9109b7981bd8feeb35c90a2da4099394421d3e9e29789f9e007f45551cd5e
BLAKE2b-256 checksum
How to use checksums
88f99933a4f514445c219c03dc22f70132fc921b4c11c8791234a7f1cf40bbb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / netpolicy-0.3.0-cp312-abi3-manylinux_2_28_aarch64.whl

Download URL netpolicy-0.3.0-cp312-abi3-manylinux_2_28_aarch64.whl
Size 257.1 kB
Tags CPython 3.12 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
ada43e18eb6a8ca47c30d04f3092fe65f65af96348f6537321d6a1c8615f9d56
BLAKE2b-256 checksum
How to use checksums
727eae1c7cbd2db921536f1c91be06c15ee5408129d58dae2fcf718fb9e7b8d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / netpolicy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl

Download URL netpolicy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl
Size 237.5 kB
Tags CPython 3.12 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e6257fd5cd600e47b66a5dc7bfa6528ecfe4b6d3b2da86207b56e93f67afb63d
BLAKE2b-256 checksum
How to use checksums
ebbd24eae697ee67046b1ca130f5918e9ec1ebe87dce4d8a75757ddb7ad3ffd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Security for netpolicy 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for netpolicy-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page