Python handles network address exception ValueError:%s has host bits set

Source: Internet
Author: User

The following exception was encountered while working with CIDR:

A = IPAddress. Ipv4network (' 10.75.74.188/24 ')

Traceback (most recent call last):
  File "<input>", line 1, in <module >
  File "C:\Users\exihaxi\AppData\Local\Programs\Python\Python36-32\lib\ipaddress.py", line 1536, in __init__
    raise ValueError ('%s has host bits set '% self)
VALUEERROR:10.75.74.188/24 has host bits set

There is no denying the fact that the address is not reasonable. But after the code has spelled out the address, the main purpose is to take the netmask, so it doesn't care about the IP part at all.

By studying the document, we can see the following description:

If Strict is True and host bits are set in the supplied address, then ValueError is raised. 
Otherwise, the host bits are masked out to determine the appropriate address.

So you can take the following solutions:

A = IPAddress. Ipv4network (' 10.75.74.188/24 ', False)
str (a.netmask)





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.