Python program to determine if the given IPv4 Address is reserved using ipaddress module


Given a IPv4 Address, the task is to determine whether it is reserved (i.e belongs to class E) or not.

What is class E?

IP addresses belonging to class E are reserved for experimental and research purposes. IP addresses of class E range from 240.0.0.0 – 255.255.255.254. This class doesn’t have any sub-net mask. The higher-order bits of the first octet of class E is always set to 1111.

Examples:

Input : 10.0.0.1
Output : Not Reserved

Input : 241.0.0.133
Output : Reserved

To implement it, we will use the is_reserved method of ipaddress module of Python3.3.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing ip_address
# from ipaddress module
from ipaddress import ip_address
  
def reservedIPAddress(IP: str) -> str:
    return "Reserved" if (ip_address(IP).is_reserved) else "Not Reserved"
      
if __name__ == '__main__'
  
    # Not Reserved
    print(reservedIPAddress('10.0.0.1')) 
      
    # Reserved
    print(reservedIPAddress('241.0.0.133')) 

chevron_right


Output :

Not Reserved
Reserved
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.