程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

IPY module of Python

編輯:Python


IPy Module introduction
IPy This powerful Python The third-party package mainly provides network segments 、 Network mask 、 Broadcast address 、 Number of subnets 、IP Type processing and other functions .

install

wget https://pypi.python.org/packages/source/I/IPy/IPy-0.81.tar.gz --no-check-certificate  # download
tar -zxvf Ipy-0.81.tar.gz # decompression
cd IPy-0.81
python setup.py install  # install 


IPy Module USES
Call module
Use IPy When the module , You need to call the module first

from IPy import IP



Define network segment

ip = IP('10.0.0.0/24')



Query the network segment IP Number
Use len() function

ip.len()
256
for i in ip.len():  # Traverse network segment IP
    print(i)



IP address translation
Use reverseName() Function pair IP Reverse parsing

ip1 = IP('192.168.4.111')
ip1.reverseName()
'111.4.168.192.in-addr.arp



see IP type
Use iptype() The function view IP type

ip1 = IP('192.168.4.111')
ip1.iptype()
PRIVATE


take IP Format conversion to other types of formats

ip1 = IP('192.168.4.111')
ip1.int()  # int() function
3232236655  
ip1.strHex()  # 16 Base number
'0xc0a8046f'
ip1.strBin()  # 2 Base number
'11000000101010000000010001101111'
print(IP(0xc0a80101))  # take 16 The base number is converted to IP Format
192.168.1.1



Judge IP The network segment

ip = IP('192.168.4.159')
Writing form 1 :
print(ip.make_net(26))
192.168.4.128/26  
Writing form 2 :
print(ip.make_net(255.255.225.192))
192.168.4.128/26  
print(IP('192.168.4.0-192.168.4.255',make_net=True))  # This network segment must be a subnet mask 255.255.225.0 The network segment
192.168.4.0/24



Different output formats of network segments
strNormal() Output IP Different formats of network segments

Parameters :

0: Output the network ID of the network segment

1: With prefix Output network segment in the form of

2: With decimalnetmask Output network segment in the format of

3: With lastIP Output network segment in the format of

print(IP('192.168.1.0/24').strNormal(0))  # Output the network ID of the network segment
192.168.1.0
print(IP('192.168.2.0/26').strNormal(1))  # Output network segment
192.168.2.0/6  # Output prefix Format segment
print(IP('192.168.3.0/24').strNormal(2))
192.168.2.0/255.255.255.192  # Output decimalnetmask Format segment
print(IP('192.168.4.0/24').strNormal(3))
192.168.2.0-192.168.2.63  # Output lastIP Format 



Judge whether the two network segments contain or overlap
Determine the network segment size
The basis of comparison : Compare the network bits , Compare values from left to right , To be able to distinguish is to compare , The non network bit must be 0

IP("10.0.0.0/24") < IP("12.0.0.0/16")  # True 12.0.0.0/16 Large network segment
IP("13.0.0.0/8") < IP("12.5.5.0/26")  # False 13.0.0.0/8 Large network segment 



Judge IP Whether the address and network segment are contained in another network segment
**in:** Judge iIP Or whether the network segment is in another network segment China

"192.168.1.55" in IP("192.168.1.0/24")  # True
IP("192.168.4.64/26") in IP("192.168.4.0/24")  # True


Determine whether the two network segments overlap

IP("192.168.4.0/26").overlaps("192.168.4.0/24")  # 1 1 Indicates that two network segments overlap
IP("10.0.0.0/26").overlaps("10.0.2.0/24")  # 0 0 Indicates that two network segments do not overlap 



Output the network address of the network segment 、 Subnet mask and broadcast address

ip = IP("192.168.6.0/24")
ip.net()  # IP('192.168.6.0') 
ip.netmask()  # IP('255.255.255.0')
ip.broadcast()  # IP('192.168.6.255')


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved