Python 3.9工作网络请求接口报[Errno 11001] getaddrinfo failed错误
问题原因分析
- 核心错误
Errno 11001 getaddrinfo failed属于DNS解析失败,仅在工作网络下运行大体积CSV时出现,说明问题出在工作网络的限制策略:短时间内大量重复DNS查询触发了公司DNS服务器/防火墙的限流规则,小文件请求量低未触及阈值、其他网络无对应限制所以运行正常。 - 当前脚本每次调用
requests.post/requests.put都会新建独立连接,每次都要重新发起DNS解析,进一步放大了DNS请求量,加速触发限流。
解决方案
1. 复用Session连接(优先级最高,改造成本最低)
使用requests的Session对象复用TCP连接,自动缓存DNS解析结果,大幅降低DNS查询次数。
2. 添加错误重试+请求间隔
捕获连接类异常,配置自动重试逻辑,同时增加每条请求的间隔,降低触发限流的概率。
3. 兜底方案:本地Hosts绑定
如果上述调整还是触发限制,可以直接在系统hosts文件添加域名IP绑定,绕开DNS查询:
# 替换为实际的域名对应IP 1xx.xx.xx.xx vaccine-verify-v1.services.school.edu
修改后完整代码
import timeit import time import requests from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry start = timeit.default_timer() url = 'https://vaccine-verify-v1.services.school.edu/api/verify' client_id = "abc" client_secret = "123" # 全局初始化Session,复用连接和DNS缓存 session = requests.Session() # 配置重试策略:连接错误、DNS失败等场景最多重试3次,重试间隔逐次增加 retry_strategy = Retry( total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504], allowed_methods=["POST", "PUT"] ) adapter = HTTPAdapter(max_retries=retry_strategy) session.mount("https://", adapter) session.mount("http://", adapter) def post_vax_record(record): manuallyVerified = record[7].lower() == "true" vaxrecord = { "firstName":record[0], "lastName": record[1], "dob": record[2], "phoneNumber": (record[3] or '4444444444'), "state": record[4], "zip": record[5][:5], "campusName": "Baltimore", "instituitionId": 0, "campusSuCd": 28, "sunyLanId": record[6], "sunyStudId": 0, "ssoId": "BEL-"+record[6], "manuallyVerified": manuallyVerified } headers = {'Client_Id':client_id, 'Client_Secret':client_secret,'Content-Type':'application/json'} try: response = session.post(url,json=vaxrecord, headers=headers, timeout=10) if response: json_response = response.json() if ('message' in json_response and json_response['message'] == 'The Student record already exists. Please use put operation to insert the record.'): print ('Post failed. Trying put instead') response = session.put(url,json=vaxrecord, headers=headers, timeout=10) if response: print('Success on put for '+record[6]) else: print('Put failed for '+record[6]) else: print('Success on post for '+record[6]) print(response.json()) else: print('Post failed for '+record[6]+' with error code: ') print(response.status_code) except Exception as e: print(f"处理记录{record[6]}时发生连接错误: {str(e)},跳过该条记录") # 每条请求后加0.3秒间隔,可根据实际运行情况调整 time.sleep(0.3) import csv with open('data_file.csv', encoding='utf-8') as csvDataFile: csvReader = csv.reader(csvDataFile) for row in csvReader: if row[6].upper().split('@')[1] in ("LIVE.LODI.EDU", "LODI.EDU"): if row[0].upper() != "FIRSTNAME": row[6] = row[6].upper().split('@')[0] post_vax_record(row) else: print('NON-LODI EMAIL FOUND FOR '+row[1]+', '+row[0]+'. WILL NOT PROCESS RECORD!') stop = timeit.default_timer() print('Run Time: ', stop - start)
内容的提问来源于stack exchange,提问作者galstar
相关产品推荐
相关产品推荐

