Home > Backend Development > Python Tutorial > Detailed explanation of Python IP address processing module

Detailed explanation of Python IP address processing module

小云云
Release: 2017-12-12 09:18:11
Original
2506 people have browsed it

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
Copy after login

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清单
Copy after login

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格式
Copy after login

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
Copy after login

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(&#39;192.168.0.0/24&#39;)<IP(&#39;192.168.1.0/24&#39;)
# 判断IP地址和网段是否包含于另一个网段中
#&#39;192.168.1.1&#39; in IP(&#39;192.168.1.0/24&#39;) 
# 判断两个网段是否存在重叠,使用overlaps()方法 
#IP(&#39;192.168.0.0/23&#39;).overlaps(&#39;192.168.1.0/24&#39;) # 返回1,表示重叠 
#IP(&#39;192.168.1.0/24&#39;).overlaps(&#39;192.168.2.0/24&#39;) # 返回0,表示没有重叠
Copy after login

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(&#39;输入IP地址或网段地址&#39;)
#ip=IP(ip_inp)
#if len(ip)>1: # 是一个网段
# print(&#39;网络地址是:%s&#39; %ip.net())
# print(&#39;子网掩码是:%s&#39; %ip.netmask())
# print(&#39;广播地址是:%s&#39; %ip.broadcast())
# print(&#39;地址反向解析:%s&#39; %ip.reverseName()[0])
# print(&#39;网络子网数为:%s&#39; %sip.len()) 
#else: # 是一个IP地址 
# print(&#39;IP地址反向解析:%s&#39; %ip.reverseName()[0])
#
#print(&#39;16进制地址:%s&#39; %ip.strHex())
#print(&#39;2进制地址:%s&#39; %ip.strBin())
#print(&#39;IP地址类型:%s&#39; %ip.iptype())
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template