zoukankan      html  css  js  c++  java
  • IPy处理IP地址

    IPy处理IP地址

    1.下载:

    pip install IPy
    

    2.方法使用:

    • 生成117.78.0.0/21网段的所有IP
    from IPy import IP
    # 117.78.0.0/21
    ip_obj = IP("117.78.0.0/21")
    # i.net() 类型为<class 'IPy.IP'>对象
    # 通过对象.strNormal   获取当前对象IP
    ip_list = [i.net().strNormal() for i in ip_obj]
    print(ip_list)
    
    • 显示IP类型
    IP("192.168.1.1").version()
    # 4
    # 为IPV4类型
    
    • 查看当网段公有多少个IP
    ip_obj = IP("117.78.0.0/21")
    print(ip_obj.len())
    # 2048
    
    • 显示IP地址是私有,还是公有
    ip = IP("192.168.1.1")
    print(ip.iptype())
    # PUBLIC 表示公有
    ip = IP("182.92.100.182")
    print(ip.iptype())
    # PRIVATE 表示私有
    
    • 反向解析地址
    ip = IP("192.168.1.1")
    print(ip.reverseName())
    # 1.1.168.192.in-addr.arpa.
    
    • 将IP转换成整型
    ip = IP("192.168.1.1")
    print(ip.int())
    # 3232235777
    
    • 将IP转换成十六进制
    print(ip.strHex())
    # 0xc0a80101
    
    • 将IP转换成二进制
    print(ip.strBin())
    # 11000000101010000000000100000001
    
    • 网路地址转化网段
    ip = IP("117.78.0.2")
    print(ip.make_net("255.255.255.0"))
    # 117.78.0.0/24
    print(IP("117.78.0.2/255.255.255.0",make_net = True))
    # 117.78.0.0/24
    
    • 判断2个网段是否重叠
    print(IP("117.78.0.0/21").overlaps("117.78.0.0/24"))
    # 1
    print(IP("117.78.0.0/21").overlaps("117.88.0.0/24"))
    # 0
    
    # 1表示重叠,0表示不重叠
    
    • 子网掩码
    ip = IP("117.78.0.2")
    print(ip.netmask())
    # 255.255.255.255
    
    • 广播地址
    print(ip.broadcast())
    
  • 相关阅读:
    LeetCode刷题--只出现一次的数
    Java进阶--多线程
    .NETCore :正则表达式
    .net core多线程:Thread
    .NETCore : Linq简介
    .NET Core:List,ArrayList和Dictionary
    .NET Core 装箱与拆箱
    .NET Core 泛型、逆变与协变
    .NET Core 类的生命周期
    .NET Core 类的多态与继承
  • 原文地址:https://www.cnblogs.com/xujunkai/p/12423237.html
Copyright © 2011-2022 走看看