gpt4 book ai didi

python - Python 的机器人框架关键字 : Error with unmatched string or IP. 如何解决此问题?

转载 作者:太空宇宙 更新时间:2023-11-03 17:24:26 27 4
gpt4 key购买 nike

我正在尝试为机器人框架编写一个关键字,正如 @Brayan Oakley 在问题中建议的那样:

How to write python function to test the matched strings (to use for Robot framework keyword)?

我的Python文件:

import os,re

def check_IP():
cmd = ' netstat -ano '
output = os.popen(cmd).read()
match1 = re.findall('.* (1.1.1.1).*',output)
mat1 = ['1.1.1.1']
if match1 == mat1:
print "IP addr found"
if match1 != mat1:
raise Exception('No matching IP...')

check_IP()

我正在尝试匹配“netstat -ano”命令中的 IP 地址。如果匹配,我将按预期收到“IP 地址已找到”消息。

但是,如果未找到 IP 地址,我会按预期收到异常,但会出现以下错误消息。

C:\Users\test\Desktop>python check.py
Traceback (most recent call last):
File "check.py", line 13, in <module>
check_IP()
File "check.py", line 11, in check_IP
raise Exception('No matching IP...')
Exception: No matching IP...

C:\Users\test\Desktop>

有什么线索可以解决这个问题吗?

最佳答案

代码完全按照您的指示执行。您正在机器人上下文之外运行代码,这就是 python 处理异常的方式。

如果您不想查看堆栈跟踪,请捕获异常并打印您想要的任何消息。

    try:
check_IP()
except Exception as e
print str(e)

当然,如果您使用 check_IP 作为关键字,您将需要删除所有这些代码。

关于python - Python 的机器人框架关键字 : Error with unmatched string or IP. 如何解决此问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32759381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com