zoukankan      html  css  js  c++  java
  • 1.Python中IP地址处理IPy模块

    安装

    先下载源码,地址:ps://pypi.python.org/pypi/IPy/">https://pypi.python.org/pypi/IPy/ ,然后解压后使用命令python setup.py install安装。

    使用

    1、显示IP类型

    >>> IP('192.168.1.1').version()
     4
     >>> IP('::1').version()
     6
    类似如上所示,通过version方法可以的判断输入的IP是IPv4还是IPv6 。

    2、网段计算输出

    代码:

    from IPy import IP

    ip=IP('192.168.0.0/28')
    print ip.len()
    for x in ip:
        print x
        
    print ip.strNormal(0)
    print ip.strNormal(1)
    print ip.strNormal(2)
    print ip.strNormal(3)
    len()方法可以计算网段的IP个数。

    strNormal()方法指定不同wantprefixlen参数可以定制不同类型的输出。上面输出类似如下:

    16
    192.168.0.0
    192.168.0.1
    192.168.0.2
    192.168.0.3
    ......
    192.168.0.15
    192.168.0.0
    192.168.0.0/28
    192.168.0.0/255.255.255.240
    192.168.0.0-192.168.0.15
    3、格式转换

    实例介绍几个常用方法,包括方向解析名称、IP类型、IP进制转换、网络地址网段地址转换。

    ip=IP('192.168.0.1')
    print ip.reverseNames()  #反向解析地址格式

    print ip.iptype()  #显示IP地址类型,私有还是公有
    ip=IP('8.8.8.8')
    print ip.iptype()

    print ip.int()  #转换成整型格式
    print ip.strHex()  #转换成十六进制格式
    print ip.strBin()  #转换成二进制格式

    #网络地址、网段地址格式转换
    print (IP('192.168.1.0').make_net('255.255.255.0'))
    print (IP('192.168.1.0/255.255.255.0',make_net=True))
    print (IP('192.168.1.0-192.168.1.255',make_net=True))
    4、地址比较

    判断IP地址和网段是否包含于另一个网段中,如下:

    >>> '192.168.1.1' in IP('192.168.1.0/24')
    True
    >>> IP('192.168.1.0/24') in IP('192.168.0.0/16')
    True
    判断两个网段是否存在重叠,如下:

    >>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')
    1
    >>> IP('192.168.1.0/24').overlaps('192.168.2.0')
    0
    其中1表示存在重叠,0表示不存在重叠。

    举例

    代码:

    #coding:utf-8

    from IPy import IP

    ip_s=raw_input("please input an IP or net-range: ")
    ips=IP(ip_s)

    if len(ips)>1:  #网络地址
        print('net: %s' % ips.net())
        print('netmask: %s' % ips.netmask())
        print('broadcast: %s' % ips.broadcast())
        print('reverse address: %s' % ips.reverseNames()[0])
        print('subnet: %s' % len(ips))
    else:  #单个地址
        print('reverse address: %s' % ips.reverseNames()[0])
        
    print('hexadecimal: %s' % ips.strHex())
    print('binary: %s' % ips.strBin())
    print('iptype: %s' % ips.iptype())
    运行结果:

    C:Usersadminworkspacezhangnq>python IPy_test2.py
    please input an IP or net-range: 192.168.1.1
    reverse address: 1.1.168.192.in-addr.arpa.
    hexadecimal: 0xc0a80101
    binary: 11000000101010000000000100000001
    iptype: PRIVATE

    C:Usersadminworkspacezhangnq>python IPy_test2.py
    please input an IP or net-range: 8.8.8.8
    reverse address: 8.8.8.8.in-addr.arpa.
    hexadecimal: 0x8080808
    binary: 00001000000010000000100000001000
    iptype: PUBLIC

    C:Usersadminworkspacezhangnq>python IPy_test2.py
    please input an IP or net-range: 192.168.1.0/28
    net: 192.168.1.0
    netmask: 255.255.255.240
    broadcast: 192.168.1.15
    reverse address: 0.1.168.192.in-addr.arpa.
    subnet: 16
    hexadecimal: 0xc0a80100
    binary: 11000000101010000000000100000000
    iptype: PRIVATE

    ipy模块用法
     

    一个自动识别IP地址、子网、方向解析、IP类型等信息的脚本

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    def ip():
    try:
    from IPy import IP ###加载模块
    ip_s = raw_input('请输入IP地址或者网段地址:' )###输入一个IP地址或者网段
    ips = IP(ip_s) #定义元素
    if len(ips) > 1: #如果len出来的数字大于1,那么就是一个网段
    print('网络地址: %s' % ips.net())
    print('子网掩码: %s' % ips.netmask())
    print('网络广播地址: %s' % ips.reverseNames() [0])
    print('网络子网数: %s' % len(ips))
    else: ###否则就是一个地址
    print('IP反向解析: %s' % ips.reverseNames() [0])
    print('十六进制地址: %s' % ips.strHex())
    print('二进制地址: %s' % ips.strBin())
    print('地址类型: %s' % ips.iptype())
    print time.strftime("%Y-%m-%d %H:%M:%S")
    #code
    except Exception, e:
    logging.info("error:" + str(e) + " " + traceback.format_exc())
    print traceback.format_exc()
    finally:
    pass


    运行效果:
     

    [root@mylinuxer python]# 192.168.1.0/24
    -bash: 192.168.1.0/24: No such file or directory
    [root@mylinuxer python]# python python.py
    请输入IP地址或者网段地址: 192.168.1.0/24
    网络地址: 192.168.1.0
    子网掩码: 255.255.255.0
    网络广播地址: 1.168.192.in-addr.arpa.
    网络子网数: 256

    [root@mylinuxer python]# python python.py
    请输入IP地址或者网段地址: 192.168.1.1
    IP反向解析: 1.1.168.192.in-addr.arpa.
    十六进制地址: 0xc0a80101
    二进制地址: 11000000101010000000000100000001
    地址类型: PRIVATE

    [root@mylinuxer python]# python python.py
    请输入IP地址或者网段地址: 116.213.249.211
    IP反向解析: 211.249.213.116.in-addr.arpa.
    十六进制地址: 0x74d5f9d3
    二进制地址: 01110100110101011111100111010011
    地址类型: PUBLIC

  • 相关阅读:
    awk 使用shell 变量
    设计模式之 外观(门面)模式 Facade
    设计模式之 抽象工厂模式
    python 第一课
    Visual Basic 图片连接地址添加
    smarty 不同模板 缓存时间
    PHP 传参过滤
    Nginx 0.7.x + PHP 5.2.10(FastCGI)搭建支持高并发量的Web服务器
    linux vi 编辑命令
    PHP 命令模式 执行文件 并传递参数
  • 原文地址:https://www.cnblogs.com/kaixiang/p/7131187.html
Copyright © 2011-2022 走看看