当提供的密码不正确时,如何停止对Jira的递归登录尝试?

我正在使用python构建一个实用程序来连接到Jira并提取TEST覆盖率。作为此工具的一部分,我要求用户输入用户凭据。该工具会等待用户输入,例如输入usid / pwd,一旦成功,则要求提供Jira查询。然后,它运行查询并提供结果。

这里的问题是,作为一个负面场景,我尝试输入了一个不正确的密码,但后来Jira本身尝试了多次使用不正确的凭据并锁定了帐户。

我们如何在第一个警告本身中停止这种重试,并捕获该警告以提醒用户检查其输入的密码/usid是否正确?我尝试了尝试/except block,但它似乎没有抓住它。

警告:root:从GET https://jira.xxxxxxcom/rest/api/2/serverInfo 获取错误,将在1.5083078521975724中重试[1/3]。错误: 401
警告:root:从 GET https://jira.xxxxxxcom/rest/api/2/serverInfo 获取了可恢复的错误,将在 35.84973140451337 中重试 [2/3]。错误: 401

我的代码如下:

pwd=input("Enter Jira credentials")

while True:

    **try:**

        jira = JIRA(options={'server': 'https://jira.dummy.com', 'verify': False}, basic_auth=(os.getlogin(), pwd))     //executing this line internally retry the same invalid credential many times

        return jira   // returns jira handle to another function to process.

        break

    **except JIRAError as e:**

        if (e.status_code == 401):

            print("Login to JIRA failed. Check your username and password")

            pwd = input("Enter your password again to access Jira OR you may close the tool ")



喵喔喔
浏览 205回答 2
2回答

蝴蝶刀刀

有点晚了,但对于其他任何寻找答案的人来说,JIRA对象的构造函数上有一个max_retries属性。            self.__jira = JIRA(                 basic_auth=(username, password),                 max_retries=0,                 options={                                     'server': 'https://jira.dummy.com/'                 }             )您可以在源代码中看到该变量和其他变量 https://jira.readthedocs.io/en/master/_modules/jira/client.html?highlight=max_retries#

慕沐林林

您似乎希望 在失败时提示用户输入有效的凭据。您不是每次尝试身份验证时都请求凭据,因此请将输入语句移动到无限循环中并尝试以下操作:while True:    pwd=input("Enter Jira credentials")    try:        jira = JIRA(options={'server': 'https://jira.dummy.com', 'verify': False}, basic_auth=(os.getlogin(), pwd))     //executing this line internally retry the same invalid credential many times        return jira   // returns jira handle to another function to process.    except JIRAError as e:        if (e.status_code == 401):            print("Login to JIRA failed. Check your username and password")            pwd = input("Enter your password again to access Jira OR you may close the tool ")这将要求您再次输入凭据,然后再使用相同的旧内容重试。而且在 try 语句中保留 return 语句后中断是没有意义的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python