IPY module originally used to enter the correct network bit and mask, I use the mechanism of processing error to achieve the input of any IP address and mask can correctly output the results of small programs.
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #@Date: 2017-09-04 21:57:154 #@Author: Enderzhou ([email protected])5 #@Link: http://www.cnblogs.com/enderzhou/6 #@Version: $Id $7 8 fromIPyImportIP9 Ten defIpip (a): One Try: AIPS =IP (a) - Printa - forIpinchIPs: the #Print IP - ifIP = =Ips[0]: - PrintSTR (ips[0]) +'\ t Network bit/the NET address' - elifIP = = ips[1]: + PrintSTR (ips[1]) +'\ t Gateway (most of the cases)/gateway (in the most cases)'#Most of the cases this English do not know whether accurate ^_^! - elifIP = = Ips[-1]: + PrintSTR (ips[-1]) +'\ t Broadcast bit/broadcast address' A Else : at PrintIP - exceptException as E: - #using the error mechanism, the last digit is reduced by an error, and the data output is realized by self-invocation to the correct network bit . -b = (A.split ('/') [0]). Split ('.') -A = B[0] +'.'+ b[1] +'.'+ b[2] +'.'+ str (int (b[-1])-1) +'/'+ A.split ('/') [-1] - Ipip (a) in - defMain (): to #The IP address and mask you need to know to enter the keyboard when executing the file +Inputip = Raw_input ('Please input IPaddress (example:192.168.1.1/24): \ n >>>') - Ipip (Inputip) the #ipip (' 10.10.10.1/30 ') #测试用例, in order to avoid repeated input comment off the above two lines, cancel the bank comments can run the test run the program directly * $ if __name__=='__main__':Panax NotoginsengMain ()
Python input IP address and mask tells you all the addresses that the network segment contains (IPY module exercises)