Python檢測(cè)端口IP字符串是否合法
IP合法性校驗(yàn)是開(kāi)發(fā)中非常常用的,看起來(lái)很簡(jiǎn)單的判斷,作用確很大,寫(xiě)起來(lái)比較容易出錯(cuò),今天我們來(lái)總結(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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于 Python 實(shí)踐感知器分類算法2. Python如何批量生成和調(diào)用變量3. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖4. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析5. Python 中如何使用 virtualenv 管理虛擬環(huán)境6. python利用opencv實(shí)現(xiàn)顏色檢測(cè)7. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫(huà)特效8. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)9. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車10. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題
