network-validators
Validators for network-related input: IP addresses, subnets, IP ranges, hostnames, ports and MAC addresses. Each check returns a stable error code, not a message, so you write the messages your users see.
- No dependencies. Python 3.11+.
- Strict about common traps: IPv4 leading zeros (CVE-2021-29921), non-ASCII digits, surrounding whitespace, IPv6 zone IDs.
- Behaves the same as the TypeScript package. Both follow one specification and run the same test cases.
Status: 0.1. Before 1.0, a minor version (0.2, 0.3, …) may include breaking changes. Every change is listed in the changelog.
Install
pip install network-validators
Usage
Every validator has two forms: validate_* returns a result, and is_* returns a bool.
from network_validators import is_host, validate_subnet
r = validate_subnet("10.0.0.5/24", allow_host_bits=False)
r.valid # False
r.code # "subnet.host_bits_set"
r.details # {"network": "10.0.0.0/24"}
if r: # a result is truthy only when valid
...
is_host("example.com") # True
Map codes to your own messages:
from network_validators import ErrorCode, validate_subnet
MESSAGES: dict[ErrorCode, str] = {
"empty": "This field is required.",
"subnet.host_bits_set": "Did you mean {network}?",
"port.out_of_range": "Port must be between {min} and {max}.",
}
r = validate_subnet(value, allow_host_bits=False)
if r.code is not None: # same as `if not r`, and type checkers then see code as an ErrorCode
message = MESSAGES.get(r.code, "Invalid value.").format(**r.details)
ErrorCode is a Literal type of every code, so a type checker flags a misspelled code in your map. ERROR_CODES lists them at runtime, for example to check that a message catalog covers them all.
Empty strings fail with code empty. If a field is optional, skip validation when it's empty.
Invalid options, such as version=5, an unknown option name or a bad entry in subnets, raise TypeError or ValueError instead of returning a result.
Validators
| Function | Accepts | Options |
|---|---|---|
validate_ip_address |
192.0.2.1, 2001:db8::1 |
version, allow_zone_id, allow_unspecified, allow_private |
validate_private_ip_address |
10.0.0.1, fd00::1 |
version, allow_zone_id, additional_ranges |
validate_netmask |
255.255.255.0 |
|
validate_subnet |
10.0.0.0/8, 10.0.0.0 255.0.0.0, 2001:db8::/32 |
version, allow_netmask, allow_host_bits |
validate_private_subnet |
192.168.1.0/24 |
as validate_subnet, plus additional_ranges |
validate_ip_range |
10.0.0.1-10.0.0.50 |
version, allow_single |
validate_ip_in_subnets / validate_ip_not_in_subnets |
an IP address | subnets (required), version |
validate_subnet_in_subnets / validate_subnet_not_overlapping |
a subnet | subnets (required), version |
validate_ip_range_not_overlapping |
an IP range | subnets (required), version |
validate_hostname / validate_fqdn |
server1 / www.example.com |
allow_wildcard, allow_underscore, allow_trailing_dot |
validate_host |
a hostname, FQDN or IP address | IP and name options, plus allow_single_label |
validate_port / validate_port_range |
443 / 8000-8080 |
allow_zero |
validate_port_list |
80, 443, 8000-8080 |
allow_zero, allow_ranges |
validate_mac_address |
00:1a:2b:3c:4d:5e, 001a.2b3c.4d5e |
formats |
version is 4, 6 or "any" (the default).
The specification gives each validator's exact rules and the order of its checks. All error codes are listed in codes.json.
License
MIT
Metadata
Release files for network-validators 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 |
|---|---|---|---|---|
| network_validators-0.1.0.tar.gz | 12.6 kB | With provenance | Details for network_validators-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| network_validators-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for network_validators-0.1.0-py3-none-any.whl |
Total release size: 29.7 kB
Release files / network_validators-0.1.0.tar.gz
| Download URL | network_validators-0.1.0.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f5b08e550b6f00076934bf8e35e4662aef23f3bd192f48906a52443071ca6e7e
|
|
BLAKE2b-256 checksum How to use checksums |
670c61610c28061e74a1d426db6a8aa2990acddce61bdc4ca0a7e8a0779d9d96
|
| 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 Nov 28, 2026.
Transparency logRelease files / network_validators-0.1.0-py3-none-any.whl
| Download URL | network_validators-0.1.0-py3-none-any.whl |
|---|---|
| Size | 17.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
68de95ef45a5b12a896caaa05e917c98601ab0b2f3437ec8c7529316c5738798
|
|
BLAKE2b-256 checksum How to use checksums |
6affc2ee51b67c1a907f6c6450f6b5f900215cdd2d32b558574a331611acf3db
|
| 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 Nov 28, 2026.
Transparency logSecurity for network-validators 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
Both files in this release have been uploaded with provenance metadata.
Attestation for network_validators-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 Nov 28, 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.