Python檢測(cè)端口IP字符串是否合法
IP合法性校驗(yàn)是開發(fā)中非常常用的,看起來很簡單的判斷,作用確很大,寫起來比較容易出錯(cuò),今天我們來總結(jié)一下,看一下3種常用的IP地址合法性校驗(yàn)的方法。
不使用正則表達(dá)式的方式:
def is_ip(ip: str) -> bool: return True if [True] * 4 == [x.isdigit() and 0 <= int(x) <= 255 for x in ip.split('.')] else False
使用正則表達(dá)式的方式
import re def isIP(str): p = re.compile(’^((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)$’) if p.match(str): return True else: return False
另一種
def checkip(hostip): pat = re.compile(r’([0-9]{1,3}).’) r = re.findall(pat,hostip+'.') if len(r)==4 and len([x for x in r if int(x)>=0 and int(x)<=255])==4: return True else: return False
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用IDEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法2. idea自定義快捷鍵的方法步驟3. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟4. Docker容器如何更新打包并上傳到阿里云5. 刪除docker里建立容器的操作方法6. Docker究竟是什么 為什么這么流行 它的優(yōu)點(diǎn)和缺陷有哪些?7. Django中如何使用Channels功能8. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法9. idea設(shè)置代碼格式化的方法步驟10. IntelliJ IDEA設(shè)置編碼格式的方法

網(wǎng)公網(wǎng)安備