Python请求Error('Connection aborted.',OSError(0,'Error'))和连接超时(以s为单位)

2024-05-28 22:57:49 发布

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

我尝试使用django和python在一个ip范围内获取redfishapi,以找出启用redfish的所有ip。当我一个接一个地查找ip时,它工作得很好,但是在规模上遇到了连接错误问题。在

这是我用来模拟错误的示例代码:

def thread_func(ip_resource, sem):
    import requests
    def get_auth_token(ip, brand, redfish_user, redfish_password):
        s = requests.Session()
        headers = {"content-type": "application/json"}
        url = "https://{}/redfish/v1/SessionService/Sessions".format(ip)
        r = s.post(
            url,
            json={"UserName": redfish_user, "Password": redfish_password},
            headers=headers,
            timeout=120,
            verify=False,
        )

        if r.status_code == 201:
            s.headers.update({'X-Auth-Token': r.headers['X-Auth-Token']})
            return r.headers['Location'], r.headers['X-Auth-Token'], s
        else:
            return "", "", None

    def delete_token(ip, location, auth_token, session):
        headers = {"content-type": "application/json", "X-Auth-Token": auth_token}
        url = "https://{}{}".format(ip, location)
        r = session.delete(
            url,
            headers=headers,
            timeout=120,
            verify=False,
        )
        session.close()
    with sem:
        print("ENTERED")
        try:
            location, token, session = get_auth_token(ip_resource.ip, ip_resource.brand, ip_resource.redfish_user, ip_resource.redfish_password)
            if location != "":
                r = session.get(
                    "https://{}/redfish/v1/Systems".format(ip_resource.ip),
                    timeout=120,
                    verify=False,
                )
                if r.status_code is None or r.status_code < 200 or r.status_code > 299:
                    print("Request 1 Error")
                    return

                computer_system_resource_url = r.json()["Members"][0]["@odata.id"]
                r2 = session.get(
                    "https://{}{}".format(ip_resource.ip, computer_system_resource_url),
                    timeout=120,
                    verify=False,
                )
                if r2.status_code is None or r2.status_code < 200 or r2.status_code > 299:
                    print("Request 2 Error")
                delete_token(ip_resource.ip, location, token, session)
            else:
                print("Cannot get token")
        except requests.exceptions.ConnectionError as e:
            print("Errors: " + str(e))
semaphore = threading.Semaphore(50)
for ip_resource in ip_resources:
    s = threading.Thread(target=thread_func, args=(ip_resource,semaphore))
    threads.append(s)
    s.start()

在规模上,我在ip_resources中有大约7k个ip。它们会随机出现连接错误。弹出的两个错误是: ('Connection aborted.', OSError(0, 'Error'))和{}

我不认为我已经达到了tcp连接的极限,因为它最多有7k个连接,而且我正在使用会话来重用连接。在


Tags: iptokenauthjsonurlgetsessionstatus

热门问题