精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

Python IP地址

Python IP地址

IP地址(Internet協(xié)議)是一種基本的網絡概念,可在網絡中提供地址分配功能。python模塊ipaddress廣泛用于驗證IP地址并將其分類為IPV4和IPV6類型。它還可以用于比較IP地址值以及用于處理ip地址的IP地址算法。

1. 驗證IPV4地址

ip_address函數(shù)驗證IPV4地址。如果值的范圍超出0到255,則將引發(fā)錯誤。

# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
print (ipaddress.ip_address(u'192.168.0.255'))
print (ipaddress.ip_address(u'192.168.0.256'))
 

當運行上面的程序時,得到以下輸出:

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
192.168.0.255
 ValueError: u'192.168.0.256' does not appear to be an IPv4 or IPv6 address

2. 驗證IPV6地址

ip_address函數(shù)驗證IPV6地址。如果值的范圍超出0到ffff,則將引發(fā)錯誤。

# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
print (ipaddress.ip_address(u'FFFF:9999:2:FDE:257:0:2FAE:112D'))
 #invalid IPV6 address
 print (ipaddress.ip_address(u'FFFF:10000:2:FDE:257:0:2FAE:112D'))
 

當運行上面的程序時,得到以下輸出:

# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
ffff:9999:2:fde:257:0:2fae:112d
 ValueError: u'FFFF:10000:2:FDE:257:0:2FAE:112D' does not appear to be an IPv4 or IPv6 address
  
 

3. 檢查IP地址的類型

我們可以提供各種格式的IP地址,并且該模塊將能夠識別有效格式。它還將指示它是IP地址的類別。

# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
print type(ipaddress.ip_address(u'192.168.0.255'))
 print type(ipaddress.ip_address(u'2001:db8::'))
 print ipaddress.ip_address(u'192.168.0.255').reverse_pointer
 print ipaddress.ip_network(u'192.168.0.0/28')
  
 

當運行上面的程序時,得到以下輸出:

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
255.0.168.192.in-addr.arpa
 192.168.0.0/28
  
 

4. IP地址比較

可以對IP地址進行邏輯比較,以確定它們是否相等。還可以比較一個IP地址的值是否大于另一個IP地址的值。

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
print (ipaddress.IPv4Address(u'192.168.0.2') > ipaddress.IPv4Address(u'192.168.0.1'))
 print (ipaddress.IPv4Address(u'192.168.0.2') == ipaddress.IPv4Address(u'192.168.0.1'))
 print (ipaddress.IPv4Address(u'192.168.0.2') != ipaddress.IPv4Address(u'192.168.0.1'))
  
 

當運行上面的程序時,得到以下輸出:

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
True
 False
 True
  
 

5. IP地址算術

還可以應用算術運算來操縱IP地址。可以在IP地址中添加或減去整數(shù)。如果相加后最后一個八位位組的值超過255,則前一個八位位組將遞增以容納該值。如果多余的值不能被任何先前的八位位組吸收,則將引發(fā)值錯誤。

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
print (ipaddress.IPv4Address(u'192.168.0.2')+1)
 print (ipaddress.IPv4Address(u'192.168.0.253')-3)
 # Increases the previous octet by value 1.
 print (ipaddress.IPv4Address(u'192.168.10.253')+3)
 # Throws Value error
 print (ipaddress.IPv4Address(u'255.255.255.255')+1)
  
 

當運行上面的程序時,得到以下輸出:

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.090948.com
# Date : 2020-08-25
192.168.0.3
 192.168.0.250
 192.168.11.0
 AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
  
 

下一節(jié):Python DNS查找

Python 網絡編程

相關文章