python 判斷參數是否爲ip格式

import re

ip_test = ['10.10.10.10', '10.666.10.10', '10.10.10']

ip_format = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')

for ip in ip_test:
    if ip_format.match(ip):
        print("It's a IP address")
    else:
       print("It's not a IP address")
# python ip_format.py
It's a IP address
It's not a IP address
It's not a IP address

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章