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 theValueErrormessages are therefore the same asipaddress. - 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.
Policyis 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.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| netpolicy-0.1.0.tar.gz | 16.6 kB | With provenance | Details for netpolicy-0.1.0.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| netpolicy-0.1.0-cp312-abi3-manylinux_2_28_x86_64.whl | CPython 3.12 | abi3 | Linux glibc 2.28+ x86-64 | Details for netpolicy-0.1.0-cp312-abi3-manylinux_2_28_x86_64.whl |
| netpolicy-0.1.0-cp312-abi3-manylinux_2_28_aarch64.whl | CPython 3.12 | abi3 | Linux glibc 2.28+ ARM64 | Details for netpolicy-0.1.0-cp312-abi3-manylinux_2_28_aarch64.whl |
| netpolicy-0.1.0-cp312-abi3-macosx_11_0_arm64.whl | CPython 3.12 | abi3 | macOS 11.0+ ARM64 | Details for netpolicy-0.1.0-cp312-abi3-macosx_11_0_arm64.whl |
Total release size: 757.5 kB
Release files / netpolicy-0.1.0.tar.gz
| Download URL | netpolicy-0.1.0.tar.gz |
|---|---|
| Size | 16.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
044755e9c57c1ced27cee548dac943a081a36141ad36b4e8776324d1988b6099
|
|
BLAKE2b-256 checksum How to use checksums |
d4f17c11510ccb95c917dce753b9893ff4bbf6493dd40a60175565a87eaabf49
|
| 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 logRelease files / netpolicy-0.1.0-cp312-abi3-manylinux_2_28_x86_64.whl
| Download URL | netpolicy-0.1.0-cp312-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 255.2 kB |
| Tags | CPython 3.12 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
8c26b8e32394a08929a34503ed4d78c3e15bd35cb2f2bd025894f47a77abd11b
|
|
BLAKE2b-256 checksum How to use checksums |
b398301d7483d4e3b27cdb7e81b152d0b082a936c4a2d0e7dac1b797039e73f1
|
| 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 logRelease files / netpolicy-0.1.0-cp312-abi3-manylinux_2_28_aarch64.whl
| Download URL | netpolicy-0.1.0-cp312-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 252.6 kB |
| Tags | CPython 3.12 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
762a2a4758d7196724641d390512fec7c13ec9ebc884e28dc2ed3b259957bd6a
|
|
BLAKE2b-256 checksum How to use checksums |
a9a0fd5b74c7589c86f08f2c3c63066ed43fed4293c92974b57cd7eabfd628fc
|
| 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 logRelease files / netpolicy-0.1.0-cp312-abi3-macosx_11_0_arm64.whl
| Download URL | netpolicy-0.1.0-cp312-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 233.0 kB |
| Tags | CPython 3.12 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
1b48efe731ca124bd31c3633be4ab273809bb441d7c87a90ad4119113bd13c71
|
|
BLAKE2b-256 checksum How to use checksums |
fcb976bfdb4f4a5e37b4c2e24eca31cf960d3bed0b54285650336a8778aaa717
|
| 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 logSecurity for netpolicy 0.1.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.
Attestation for netpolicy-0.1.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 logA 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.