【发布时间】:2022-11-01 11:34:10
【问题描述】:
如何使用 Robot Framework 验证 IP 地址
例子:192.168.101.12
条件:
- 字符数
- 字符串不应超过 15 个字符
- 只允许数字字符
【问题讨论】:
标签: python selenium user-interface robotframework
如何使用 Robot Framework 验证 IP 地址
例子:192.168.101.12
条件:
【问题讨论】:
标签: python selenium user-interface robotframework
内置库有一个匹配正则表达式的关键字。您可以使用Should Match Regexp 来验证ip。这是我使用来自 answer 的正则表达式制作的示例
***Variables***
${correct_ip} 192.168.101.12
${false_ip} 999.999.999.999
${ip_regexp} ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
*** Test Cases ***
test
Should Match Regexp ${correct_ip} ${ip_regexp}
Should Not Match Regexp ${false_ip} ${ip_regexp}
【讨论】:
您可以使用正则表达式来验证 ips。
import re
def isValidIp(ip):
pattern = "((d{1,3}).(d{1,3}).(d{1,3}).(d{1,3}))"
if re.match(pattern, ip):
return True
else:
return False
测试:
test = ["123.123.123","randomstring","123.a54.12","1234.12.1111"]
for item in test:
print(isValidIp(item))
True
False
False
True
【讨论】: