前言
給你一個字符串,你怎么判斷是不是ipv4地址?手寫這段代碼,並寫出測試用例
判斷是不是ipv4地址
先要知道 ipv4 地址的格式:(1~255).(0~255).(0~255).(0~255)
從格式上看有3個點,分四個部分,第1部分范圍是1~255, 后面3個部分是0~255
於是可以把字符串按.去切割,判斷數字范圍大小
'''
給你一個字符串,你怎么判斷是不是ipv4地址?手寫這段代碼,並寫出測試用例
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
'''
def check_ipv4(ip_str):
"""檢查ipv4地址格式"""
if not isinstance(ip_str, str):
print("輸入參數不合法,請輸入字符串")
return False
if ip_str.count(".") != 3:
return False
else:
ip_list = ip_str.split(".")
print(ip_list) # 按點切割四個部分
# 校驗每個部分的合法性,判斷是不是數字
num1 = ip_list[0]
try:
n1 = int(num1)
print("第1個數字:", n1)
if n1 >=1 and n1 <= 255:
pass
else:
return False
except:
return False
# 后面3個數字
for n in ip_list[1:]:
try:
n2 = int(n)
print("后面數字:", n2)
if n2 >=0 and n2 <= 255:
pass
else:
return False
except:
return False
return True
if __name__ == '__main__':
result = check_ipv4("1.2.2.3")
print(result)
正則判斷ipv4地址
也可以用正則表達式判斷ipv4地址, 正則匹配代碼會簡單很多
先第一部分匹配1-255, 匹配有5種情況
- 1位數 1-9
- 2位數 10-99
- 3位數 100-199
- 3位數 200-249
- 3位數 250-255
於是正則表達式可以寫成[1-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]
后面3個數是從0-255,根上面的差不多,把1-9 改成 0-9 即可
import re
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
def check_ipv4_by_re(ip_str):
"""正則判斷ipv4"""
if not isinstance(ip_str, str):
print("輸入參數不合法,請輸入字符串")
return False
compile_ipv4 = re.compile('^([1-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))(\\.([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))){3}$')
if compile_ipv4.match(ip_str):
return True
else:
return False
if __name__ == '__main__':
result2 = check_ipv4_by_re("255.0.2.3")
print(result2)
測試用例
什么是有效的ipv4地址?(baidu搜來的)
有效ipV4(第4版)地址的范圍是1.0.0.0~254.255.255.254,由4節組成,每節取值范圍0~255
但有一些地址是特殊的,不能算bai作”有效值“有效IP地址的范圍是:網絡地址+1~廣播地址-1。
有效IP地址分為3類,分別是:
A類IP段 0.0.0.0 到127.255.255.255 (0段和127段不使用)。
B類IP段 128.0.0.0 到191.255.255.255
C類IP段 192.0.0.0 到223.255.255.255
如果僅僅是校驗ipv4 地址的格式:(1~255).(0~255).(0~255).(0~255)
,用上面的代碼就行了,如果要校驗有效IP,還得排除掉無效的ip
測試用例設計
測試點 | 用戶輸入 | 期望結果 |
---|---|---|
非字符串 | 1234 | 輸入不合法 False |
空字符 | "" | False |
格式不對 | abcdef | False |
格式不對 | 123.456 | False |
格式不對 | 1.2.3 | False |
第1位校驗 | a.1.1.1 | False |
- | 0.1.1.1 | False |
- | 1.1.1.1 | True |
- | 9.1.1.1 | True |
- | 10.1.1.1 | True |
- | 99.1.1.1 | True |
- | 100.1.1.1 | True |
- | 199.1.1.1 | True |
- | 200.1.1.1 | True |
- | 250.1.1.1 | True |
- | 255.1.1.1 | True |
- | 256.1.1.1 | False |
- | 356.1.1.1 | False |
第2位校驗 | 10.a.1.1 | False |
- | 10.0.1.1 | True |
- | 10.255.1.1 | True |
- | 10.256.1.1 | False |
- | 10.300.1.1 | False |
第3位校驗 | 10.0.a.1 | False |
- | 10.0.0.1 | True |
- | 10.0.255.1 | True |
- | 10.0.256.1 | False |
- | 10.0.300.1 | False |
第4位校驗 | 10.0.0.a | False |
- | 10.0.0.0 | True |
- | 10.0.0.255 | True |
- | 10.0.0.256 | False |
- | 10.0.0.300 | False |
代碼測試用例
用pytest框架測試上面的用例
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
# 測試數據
test_data = [
[1234, False],
["", False],
["abcdef", False],
["123.456", False],
["1.2.3", False],
["a.1.1.1", False],
["0.1.1.1", False],
["1.1.1.1", True],
["9.1.1.1", True]
]
@pytest.mark.parametrize("test_input, expected", test_data)
def test_ip(test_input, expected):
assert expected == check_ipv4_by_re(test_input)
用例太多,省略了一部分