我正在尝试使用django和python在IP范围内抓取Redfish API,以找出启用了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 ips。并且他们将随机出现连接错误。弹出的两个错误是:
('Connection aborted.', OSError(0, 'Error'))
和HTTPSConnectionPool(host='x.x.x.x', port=443): Max retries exceeded with url: /redfish/v1/SessionService/Sessions (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7fca78229208>, 'Connection to x.x.x.x timed out. (connect timeout=120)'))
我认为我没有达到tcp连接的限制,因为它最多是7k连接,并且我正在使用会话来重用连接。