Python input IP address and mask tells you all the addresses that the network segment contains (IPY module exercises)

Source: Internet
Author: User


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)

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.