Python -- IPY Library () classification: pythonwebsite: IPY >>>> from IPY import IP >>> Dir (IP) ['_ add __', '_ CMP _', '_ ins INS _', '_ Doc _', '_ eq _', '_ getitem __', '_ Hash _', '_ init _', '_ Len _', '_ LT _', '_ module __', '_ nonzero _', '_ repr _', '_ STR _', '_ getipv4map', '_ printprefix', 'broadcast ', 'int', 'iptype', 'len', 'Make _ net', 'net', 'netmask', 'overlaps', 'prefixlen', 'reversename ', 'reversenames ', 'strbin', 'strcomputed', 'strdec', 'strfullsize', 'strhex', 'strnetmask', 'strnormal ', 'version'] >>> IP ('123. 29.20.80/28 '). len () -- number of IP addresses 16 >>> IP address ('100. 172. 29.20.80/28 '). net () -- IP address of the CIDR Block ('20180101. 29.20.80') >>> IP ('123. 29.20.80/28 '). netmask () -- mask IP address ('123. 201710000240') >>> IP ('20180101. 29.20.0/24 '). prefixlen () -- mask, int type 24 >>> IP ('100%. 29.20.0/24 '). strnormal (0) -- CIDR Block '2017. 29.20.0 '> IP address ('2017. 29.20.0/24 '). strnormal (1) -- network segment + mask '000000. 29.20.0/24 '>>> IP address ('2017. 29.20.0/24 '). strnormal (2) -- network segment + mask '123. 29.20.0/255.255.255.0 '> IP address ('2017. 29.20.0/24 '). strnormal (3) -- network segment + mask '123. 29.20.0-172.29.255.255 '> IP address ('100. 172.29.255.255'>. 29.20.0/24 '). strnetmask () -- mask '100. 255.255.0 '>>> IP ('123. 29.20.80/28 '). strnetmask () -- mask '100. 255.255.240' >>> IP ('123. 29.20.0/24 '). version () -- IP v4 or V6 version 4 >>> '2017. 0.0.1 'in IP ('100. 127. 0.0.0/24 ') True >>> IP ('127. 0.0.0/24 ') in IP ('2014. 127. 0.0.0/25 ') False >>> print (IP ('2017. 168.1.1 '). iptype () Private >>> print (IP ('100. 152. 168.1.1 '). iptype () Public >>> help (IP) -- you can see the most detailed documentation, more methods >>> IP = IP ('123. 0.0.0/30') >>> for I in IP :... print (I )... 127.0.0.0127.0.0.1127.0.0.2127.0.0.3> for I in IP :... print (Type (I ))... <type 'instance'> <type 'instance' >>> print (IP) 127.0.0.0/30 >>> for I in IP :... print (STR (I ))... 127.0.0.0127.0.0.1127.0.0.2127.0.0.3> Print (IP [2]) 127.0.0.2> Print (STR (IP [2]) 127.0.0.2 other common methods: | _ CMP _ (self, other) -- compare the size | called by comparison operations. | shoshould return a negative integer if self <other, zero if self | = Other, a positive integer if self> Other. | networks with different prefixlen are considered non-equal. | networks with the same prefixlen and differing addresses are | considered non equal but are compared by their base address | integer value to aid sorting of IP objects. | the version of objects is not put into consideration. | >>> IP ('10. 0.0.0/24 ')> IP ('10. 0.0.0 ') | 1 | >>> IP ('10. 0.0.0/24 ') <IP ('10. 0.0.0 ') | 0 | >>> IP ('10. 0.0.0/24 ') <IP ('12. 0.0.0/24 ') | 1 | >>> IP ('10. 0.0.0/24 ')> IP ('12. 0.0.0/24 ') | 0 | _ contains _ (self, item) -- check the inclusion relationship | called to implement membership test operators. | shocould return true if item is in self, false otherwise. item | can be other IP-objects, strings or ints. | >>> IP address ('123. 185.1.1 '). strhex () | '0xc3b90101 '| >>> 0xc3b90101 in IP address ('2017. 185.1.0/24 ') | true |>' 127. 0.0.1 'in IP ('100. 127. 0.0.0/24 ') | true | >>> IP ('2017. 0.0.0/24 ') in IP ('2014. 127. 0.0.0/25') | false | overlaps (self, item) -- check for overlap | check if two IP address ranges overlap. | returns 0 if the two ranges don't overlap, 1 if the given | range overlaps at the end and-1 if it does at the beginning. | >>> IP address ('123. 168.0.0/23 '). overlaps ('2017. 168.1.0/24 ') | 1 | >>> IP ('2017. 168.0.0/23 '). overlaps ('2017. 168.1.255 ') | 1 | >>> IP ('2017. 168.0.0/23 '). overlaps ('2017. 168.2.0 ') | 0 | >>> IP ('100. 192. 168.1.0/24 '). overlaps ('2017. 168.0.0/23') |-1
Calculate the CIDR block address and broadcast address (original) based on the IP address and subnet mask. in Python, there is an IP class library dedicated to this type of problem. Let's take a look:
View sourceprint? 01 #! /Usr/bin/ENV Python
02
03 import sys
04 from IPY import IP
05
06 address = SYS. argv [1]
07 netmask = SYS. argv [2]
08
09 # print address, netmask
10
11 # Help (IP)
12 networkaddress = IP (Address). make_net (netmask) # init a IP instance, can with netmask directly, or use make_net (netmask)
13 bcastaddress = IP (networkaddress). Broadcast () # Return the broadcast IP Address
14
15 print networkaddress
16 print bcastaddress