# How to get network address using ipaddress?

**URL:** <https://discuss.python.org/t/how-to-get-network-address-using-ipaddress/10528>\
**Category:** Python Help\
**Tags:** help\
**Created:** [September 9, 2021, 12:53am UTC](https://discuss.python.org/t/how-to-get-network-address-using-ipaddress/10528 "2021-09-09T00:53:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamaszl](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tamaszl/32/4880_2.png) [@tamaszl](https://discuss.python.org/u/tamaszl)\
**Post date:** [September 9, 2021, 12:53am UTC](https://discuss.python.org/t/how-to-get-network-address-using-ipaddress/10528/1 "2021-09-09T00:53:48Z")

</div>

```auto
network = ipaddress.IPv4Network("192.168.1.1/24")
print("Network address: ", network.network_address)

```

> ```
> network = ipaddress.IPv4Network("192.168.1.1/24")
> 
> ```
> 
> File “/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ipaddress.py”, line 1454, in **init**  
> raise ValueError(‘%s has host bits set’ % self)  
> ValueError: 192.168.1.1/24 has host bits set

This returns an error. If I change the IP address to 192.168.1.0/24 then it’ll return 192.168.1.0/24. Isn’t this function supposed to return 192.168.1.0/24 when I give it 192.168.1.1/24?

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [September 9, 2021, 1:21am UTC](https://discuss.python.org/t/how-to-get-network-address-using-ipaddress/10528/2 "2021-09-09T01:21:45Z")

</div>

The default is that `strict=True`

From [ipaddress](https://docs.python.org/3/library/ipaddress.html?highlight=ipaddress#ipaddress.IPv4Network)

> _class_ `ipaddress.` `IPv4Network` ( _address_ , _strict=True_ )  
> […]  
> If _strict_ is `True` and host bits are set in the supplied address, then [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) is raised. Otherwise, the host bits are masked out to determine the appropriate network address.

So:

```python
>>> ipaddress.IPv4Network("192.168.1.1/24", strict=False)
IPv4Network('192.168.1.0/24')

```

---

<div class="post-metadata">

**Author:** ![tamaszl](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tamaszl/32/4880_2.png) [@tamaszl](https://discuss.python.org/u/tamaszl)\
**Post date:** [September 9, 2021, 2:30am UTC](https://discuss.python.org/t/how-to-get-network-address-using-ipaddress/10528/3 "2021-09-09T02:30:34Z")

</div>

Thanks for the answer. I wish they would make an explanation that is easier to understand.
