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

2024-04-20 08:04:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这里的问题是,作为一个消极的场景,我试图输入一个错误的密码,但是Jira自己使用错误的凭据尝试了多次,并锁定了帐户

我们如何在第一个警告中停止此重试并捕获该警告以提醒用户检查其密码/usid输入是否正确?我试过了,但它似乎没抓住

WARNING:root:Got recoverable error from GET https://jira.xxxxxxcom/rest/api/2/serverInfo, will retry [1/3] in 1.5083078521975724s. Err: 401
WARNING:root:Got recoverable error from GET https://jira.xxxxxxcom/rest/api/2/serverInfo, will retry [2/3] in 35.84973140451337s. Err: 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 ")

Tags: 工具to用户https警告密码错误pwd
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语句中保留break after return语句是没有意义的

相关问题 更多 >