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. A bare string may contain comma-separated networks. Allowed ranges are carved out of denied networks. The remaining ranges are summarized into the smallest possible set of networks, sorted as strings and joined with commas.

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"])  # more networks to allow

Build Policy once and keep it for as long as its input stays the same. render_excluding() returns the comma-separated block list with the additional allowed networks removed. If none of them overlap the policy, the result is unchanged.

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.

Behaviour

  • Plain IPs and numeric prefixes are parsed in Rust. Anything else, such as netmask notation, scope IDs or invalid input, goes through ipaddress.ip_network(ip, strict=False). The accepted input and the ValueError messages are therefore the same as ipaddress.
  • IPv4-mapped IPv6 addresses are formatted as ::ffff:1.2.3.4, like Python 3.13 and newer do.
  • The GIL is released while ranges are carved and formatted.
  • Policy is immutable, so one instance can be shared between threads.

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

Metadata

Release files for netpolicy 0.2.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.2.0
File Size Uploaded Provenance Actions
netpolicy-0.2.0.tar.gz 17.7 kB With provenance Details for netpolicy-0.2.0.tar.gz

Built distributions (wheels)

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

Total release size: 761.9 kB

Release files / netpolicy-0.2.0.tar.gz

Download URL netpolicy-0.2.0.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
38a0e68c9c1c0b602529c26195ec8e8c21d84696c5527f74aa58b31cb02d195c
BLAKE2b-256 checksum
How to use checksums
7a4ceb40ae54a858ecb7a7fa2142785c12bb24346fa23236f3fd2bea2d461563
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 5, 2026.

Transparency log

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

Download URL netpolicy-0.2.0-cp312-abi3-manylinux_2_28_x86_64.whl
Size 256.3 kB
Tags CPython 3.12 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
4637cb30dab053305e8cf8cd51cc02b6b3ed073e91ae1250c88029d890c83244
BLAKE2b-256 checksum
How to use checksums
e749798828cdf7a2b486db54e9050830db42aaed75b4579b74879a506a077be0
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 5, 2026.

Transparency log

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

Download URL netpolicy-0.2.0-cp312-abi3-manylinux_2_28_aarch64.whl
Size 253.8 kB
Tags CPython 3.12 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
35fae06fd6346a38b180926caf8ebbf11df26875f227a465d0356ce686c43870
BLAKE2b-256 checksum
How to use checksums
4bcdd0bb2f56b57c2b64713eda5d0dda75b84cd4bffae27d2c9ca9d983fe356d
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 5, 2026.

Transparency log

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

Download URL netpolicy-0.2.0-cp312-abi3-macosx_11_0_arm64.whl
Size 234.2 kB
Tags CPython 3.12 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2a8107fde1dc25824032b6ba827bbae76b9442301527763338738a83dc035efa
BLAKE2b-256 checksum
How to use checksums
c547b10abfd507d07bbdffc858d47667cff02543625b95923effaf6006903f17
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 5, 2026.

Transparency log

Security for netpolicy 0.2.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.2.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 5, 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