Python IP地址
Python IP地址
IP地址(Internet協(xié)議)是一種基本的網(wǎng)絡(luò)概念,可在網(wǎng)絡(luò)中提供地址分配功能。python模塊ipaddress廣泛用于驗(yàn)證IP地址并將其分類為IPV4和IPV6類型。它還可以用于比較IP地址值以及用于處理ip地址的IP地址算法。
1. 驗(yàn)證IPV4地址
ip_address函數(shù)驗(yàn)證IPV4地址。如果值的范圍超出0到255,則將引發(fā)錯(cuò)誤。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # Date : 2020-08-25 print (ipaddress.ip_address(u'192.168.0.255')) print (ipaddress.ip_address(u'192.168.0.256'))
當(dāng)運(yùn)行上面的程序時(shí),得到以下輸出:
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # 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. 驗(yàn)證IPV6地址
ip_address函數(shù)驗(yàn)證IPV6地址。如果值的范圍超出0到ffff,則將引發(fā)錯(cuò)誤。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # 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'))
當(dāng)運(yùn)行上面的程序時(shí),得到以下輸出:
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # 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地址,并且該模塊將能夠識(shí)別有效格式。它還將指示它是IP地址的類別。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # 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')
當(dāng)運(yùn)行上面的程序時(shí),得到以下輸出:
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # Date : 2020-08-25 255.0.168.192.in-addr.arpa 192.168.0.0/28
4. IP地址比較
可以對(duì)IP地址進(jìn)行邏輯比較,以確定它們是否相等。還可以比較一個(gè)IP地址的值是否大于另一個(gè)IP地址的值。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # 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'))
當(dāng)運(yùn)行上面的程序時(shí),得到以下輸出:
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # Date : 2020-08-25 True False True
5. IP地址算術(shù)
還可以應(yīng)用算術(shù)運(yùn)算來操縱IP地址。可以在IP地址中添加或減去整數(shù)。如果相加后最后一個(gè)八位位組的值超過255,則前一個(gè)八位位組將遞增以容納該值。如果多余的值不能被任何先前的八位位組吸收,則將引發(fā)值錯(cuò)誤。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # 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)
當(dāng)運(yùn)行上面的程序時(shí),得到以下輸出:
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : # 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
相關(guān)文章
- python字符串定義的方式有哪些
- python中f字符串以及其常見用法介紹
- Python的gtts庫將文字轉(zhuǎn)為音頻應(yīng)該如何操作
- 使用Python?Beautiful?Soup解析HTML內(nèi)容的方法
- 在Python里面調(diào)用Golang代碼的方法
- Python異步之在Asyncio中怎么運(yùn)行阻塞任務(wù)
- Python異步之如何獲取當(dāng)前和正在運(yùn)行任務(wù)
- python如何實(shí)現(xiàn)簡(jiǎn)易的學(xué)生信息管理系統(tǒng)
- Python數(shù)據(jù)可視化之Pyecharts如何使用
- Python Internet 協(xié)議模塊
- Python DNS查找
- Python HTTP響應(yīng)
- Python 請(qǐng)求狀態(tài)代碼
- Python SMTP
- Python Web服務(wù)器
- Python 代理服務(wù)器
- Python 并發(fā)簡(jiǎn)介
- Python 線程并發(fā)
- Python 測(cè)試線程應(yīng)用程序
- Python 調(diào)試線程應(yīng)用程序