zoukankan      html  css  js  c++  java
  • Python 输入IP地址及掩码告诉你该网段包含的全部地址(IPy模块练习)


    IPy模块原本使用时需要输入正确的网络位和掩码,我利用处理报错的机制实现了输入任意IP地址和掩码均可正确输出结果的小程序。

     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 # @Date    : 2017-09-04 21:57:15
     4 # @Author  : EnderZhou (zptxwd@gmail.com)
     5 # @Link    : http://www.cnblogs.com/enderzhou/
     6 # @Version : $Id$
     7 
     8 from IPy import IP
     9 
    10 def ipip(a):
    11     try:
    12         ips = IP(a)
    13         print a
    14         for ip in ips:
    15             # print ip 
    16             if ip == ips[0]:
    17                 print str(ips[0]) + '	网络位/The network address'
    18             elif ip == ips[1]:
    19                 print str(ips[1]) + '	网关(大多数情况下)/Gateway(in most cases)'#大多数情况下这个英文不知道是否准确^_^!
    20             elif ip == ips[-1]:
    21                 print str(ips[-1]) + '	广播位/Broadcast address'
    22             else :
    23                 print ip
    24     except Exception as e:
    25         #利用报错机制,在报错时对最后一位数值进行减一操作,利用自我调用尝试到正确的网络位实现数据输出
    26         b = (a.split('/')[0]).split('.')
    27         a = b[0] + '.' + b[1] + '.' + b[2] + '.' + str(int(b[-1])-1) + '/' + a.split('/')[-1]
    28         ipip(a)
    29 
    30 def main():
    31     #执行文件时键盘输入所需要知道的ip地址及掩码
    32     inputip = raw_input('please input IPaddress(example:192.168.1.1/24):
     >>>')
    33     ipip(inputip)
    34     # ipip('10.10.10.1/30')#测试用例,为了避免重复输入注释掉上面两行,取消本行注释即可直接运行本程序测试运行情况
    35 
    36 if __name__ == '__main__':
    37     main()
  • 相关阅读:
    第三次作业-有进度条圆周率计算
    第一周作业
    24点
    Cuber Sorting
    P1827 [USACO3.4]美国血统 American Heritage
    P4387 【深基15.习9】验证栈序列
    P2058 海港
    P4017 最大食物链计数
    P2196 挖地雷
    放苹果问题
  • 原文地址:https://www.cnblogs.com/enderzhou/p/7476378.html
Copyright © 2011-2022 走看看