What happens if a host portion remains in CIDR? – Comparing strict=True/False in Python's ipaddress

ネットワーク・RFCカテゴリを表すパンダのイラスト Networking / RFC

About This Article
This article is generated via an automated generation workflow utilizing generative AI. We review the ipaddress specifications in the official Python documentation and observe the differences when loading a CIDR with a remaining host portion using strict=True/False, using dummy addresses only. No Python execution was performed during the creation of this article.

Verification Status: 📘 Python official specification checked, execution unverified
Information verification date: October 3, 2026. No external communication or network configuration changes are performed.

192.168.1.10/24When notation including a host portion, such as, is received as a "network", whether to treat it as an input error and stop,192.168.1.0/24or to normalize it to, is crucial for tool design.

Stopping with strict=True

import ipaddress

ipaddress.ip_network("192.168.1.10/24", strict=True)

Because the host portion is configured, it is strictly rejected as a network address, resulting in .ValueError.

What happens if strict=False?

import ipaddress

net = ipaddress.ip_network("192.168.1.10/24", strict=False)
print(net)
print(net.network_address)
print(net.broadcast_address)

You can handle the input by normalizing it to a network.

flowchart TD
 A["192.168.1.10/24"] --> B{"strict?"}
 B -->|True| C["ValueError"]
 B -->|False| D["192.168.1.0/24へ正規化"]

Change one place

192.168.1.0/24, and verify that it passes even with strict=True. Next, change it to , and execute after predicting which network it will normalize to.10.0.0.1/30Next, change it to, and run it after predicting which network it will be normalized to.

For Professional Use

When validating configuration values, decide in advance whether "input errors may be silently corrected." Strict=True may be appropriate if you want to strictly inspect user-input network definitions, while strict=False may be suitable if you want to determine the target network from an IP and prefix.

Official and Primary Sources

GitHub Sample

Document information

Article title
What happens if a host portion remains in CIDR? – Comparing strict=True/False in Python's ipaddress
Published
Updated
Source
https://papanda925.com/?p=17800&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL