Home >Backend Development >Python Tutorial >Detailed explanation of Python IP address processing module
This article mainly explains the Python IP address processing module. IPy This module can easily handle IPv4 and IPv6 addresses. The following article mainly introduces relevant information about the IP address processing module of Python automated operation and maintenance. The article uses sample code The introduction is very detailed and has certain reference value for everyone's study or work. Friends who need it can take a look below. I hope it can help everyone.
Practical IP address processing module IPy
In IP address planning, it involves calculating a large number of IP addresses, including network Segment, network mask, broadcast address, number of subnets, IP type, etc.
Don’t worry, Ipy module saves you. The Ipy module can very well assist us in completing IP planning work efficiently.
Function: Assist us to complete IP planning work efficiently
Installation:
wget https://pypi.python.org/packages/source/I/IPy/IPy-0.81.tar.gz --no-check-certificate tar -zxvf Ipy-0.81.tar.gz cd IPy-0.81 python setup.py install
Basic processing of IP address and network segment:
Usage:
#from IPy import IP #ip_1 = IP('192.168.1.0/24') #print(ip_1.len()) # 输出192.168.1.0/24网段的IP个数 #for a in ip_1: # print(a) # 输出192.168.1.0/24网段的所有IP清单
Conversion of IP address:
#from IPy import IP #ip_2 = IP('192.168.1.1') #ip_2.reverseNames() # 反响解析地址格式 #ip_2.iptype() # 查看IP地址类型 #ip_2.int() # 将格式转换为整型格式 #ip_2.strHex() # 将格式转换为16进制格式 #ip_2.strBin() # 将格式转换为2进制格式 #print(IP(0x8188808)) # 将16进制转化为IP格式
IP network segment Conversion:
#from IPy import IP # 输出192.168.1.0/24 #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)) # 通过strNormal() 方法指定上述三种格式的输出: #print(IP('192.168.1.0/24').strNormal(0)) # 参数(wantprefixlen)为0,无返回,输出192.168.1.0 #print(IP('192.168.1.0/24').strNormal(1)) # 参数(wantprefixlen)为1,prefix格式,输出192.168.1.0/24 #print(IP('192.168.1.0/24').strNormal(2)) # 参数(wantprefixlen)为2,decimalnetmask格式,输出192.168.1.0/255.255.255.0 #print(IP('192.168.1.0/24').strNormal(3)) # 参数(wantprefixlen)为3,lastIP格式,输出192.168.1.0-192.168.1.255
Multi-network calculation method:
Function: Compare two network segments for inclusion, overlap, etc., such as 192.168.1.0/24 and 192.168.1.0/25; 192.168.0.0/24 and 192.168.1.0/24
Usage:
#from IPy import IP #IP('192.168.0.0/24')<IP('192.168.1.0/24') # 判断IP地址和网段是否包含于另一个网段中 #'192.168.1.1' in IP('192.168.1.0/24') # 判断两个网段是否存在重叠,使用overlaps()方法 #IP('192.168.0.0/23').overlaps('192.168.1.0/24') # 返回1,表示重叠 #IP('192.168.1.0/24').overlaps('192.168.2.0/24') # 返回0,表示没有重叠
According to the input IP address or network segment address, return the network address, broadcast address, address response analysis, subnet number, IP type and other information:
#from IPy import IP #ip_inp=raw_input('输入IP地址或网段地址') #ip=IP(ip_inp) #if len(ip)>1: # 是一个网段 # print('网络地址是:%s' %ip.net()) # print('子网掩码是:%s' %ip.netmask()) # print('广播地址是:%s' %ip.broadcast()) # print('地址反向解析:%s' %ip.reverseName()[0]) # print('网络子网数为:%s' %sip.len()) #else: # 是一个IP地址 # print('IP地址反向解析:%s' %ip.reverseName()[0]) # #print('16进制地址:%s' %ip.strHex()) #print('2进制地址:%s' %ip.strBin()) #print('IP地址类型:%s' %ip.iptype())
Related recommendations:
Case analysis of Python implementation of outputting colored strings
Python code to implement genetic algorithm
Example of Python reference pass-by-value concept
The above is the detailed content of Detailed explanation of Python IP address processing module. For more information, please follow other related articles on the PHP Chinese website!