Python requests调用API遇gaierror及无输出问题求助
问题排查与调试方法咨询
问题背景
- 使用Python的requests库向服务器发送携带文件的POST请求,最初遇到
socket.gaierror: [Errno 11001] getaddrinfo failed错误,无法建立连接 - 修改代码添加代理并禁用
InsecureRequestWarning后,程序运行无任何输出,无法获取响应状态码,需要排查原因 - 同时咨询:通用的错误调试方法是否以谷歌搜索为主?
初始代码
import requests headers = { 'authorization': <token>, } files = { 'source': open('ilo5.zip', 'rb'), 'metadata': open('metadata.json', 'rb'), } response = requests.post('https://<blablah>:443/stross/initiateScan', headers=headers, files=files, verify=False) print("hi")
初始错误信息
Traceback (most recent call last): File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\connection.py", line 174, in _new_conn conn = connection.create_connection( File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\util\connection.py", line 72, in create_connection for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM): File "C:\Program Files\Python310\lib\socket.py", line 955, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 703, in urlopen httplib_response = self._make_request( File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 386, in _make_request self._validate_conn(conn) File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 1042, in _validate_conn conn.connect() File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\connection.py", line 358, in connect self.sock = conn = self._new_conn() File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\connection.py", line 186, in _new_conn raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000022FD119A350>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\requests\adapters.py", line 489, in send resp = conn.urlopen( File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 787, in urlopen retries = retries.increment( File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\urllib3\util\retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='<blablah>', port=443): Max retries exceeded with url: /stross/initiateScan (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000022FD119A350>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed')) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\bose\Desktop\stross\stross-api-call.py", line 12, in <module> response = requests.post('<blablah>:443/stross/initiateScan', headers=headers, files=files, verify=False) File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\requests\api.py", line 115, in post return request("post", url, data=data, json=json, **kwargs) File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\requests\sessions.py", line 587, in request resp = self.send(prep, **send_kwargs) File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\requests\sessions.py", line 701, in send r = adapter.send(request, **kwargs) File "C:\Users\bose\AppData\Roaming\Python\Python310\site-packages\requests\adapters.py", line 565, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='<blablah>', port=443): Max retries exceeded with url: /stross/initiateScan (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000022FD119A350>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
修改后的代码
import requests import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) proxies = { 'http': 'http://<>.net:8080', 'https': 'http://<>.net:8080', } headers = { 'authorization': <token>, } files = { 'source': open('ilo5.zip', 'rb'), 'metadata': open('metadata.json', 'rb'), } response = requests.post('https://<>.net:443/stross/initiateScan', headers=headers, files=files, verify=False, proxies=proxies) print("hi. Response Status Code:",response.status_code)
排查步骤与解决方案
初始socket.gaierror错误分析
这个错误是DNS解析失败,核心是无法将<blablah>域名转化为对应的IP地址,可能原因:
- 域名拼写错误,检查目标地址是否正确
- 本地DNS配置异常,无法解析目标域名,可尝试更换DNS服务器(比如用8.8.8.8)
- 目标域名仅在内网环境可访问,而你的机器不在对应网络中
修改后无输出问题排查
程序无输出,说明requests.post()调用没有完成,一直处于等待状态,常见原因及解决方法:
- 代理配置错误:检查代理地址是否正确,确认代理是否允许访问目标服务器;如果代理需要认证,要在URL中加入用户名和密码,格式为
http://username:password@proxy.net:8080 - 未设置超时时间:requests默认无超时限制,会一直等待响应。添加
timeout参数(例如timeout=30),超时后会抛出异常,便于定位问题 - 文件读取异常:确保
ilo5.zip和metadata.json文件存在,路径正确(建议用绝对路径);同时改用with语句管理文件,避免资源泄漏,示例:with open('ilo5.zip', 'rb') as f1, open('metadata.json', 'rb') as f2: files = {'source': f1, 'metadata': f2} response = requests.post(...) - 网络连通性问题:代理到目标服务器的网络不通,或者服务器拒绝了代理请求,可以用curl命令测试请求是否可行:
curl -X POST -H "authorization: <token>" -F "source=@ilo5.zip" -F "metadata=@metadata.json" -x http://<>.net:8080 -k https://<>.net:443/stross/initiateScan
通用调试建议
- 测试代理可用性:先发送简单请求测试代理是否能正常工作,比如:
test_response = requests.get('https://www.example.com', proxies=proxies, verify=False, timeout=10) print(test_response.status_code) - 开启调试日志:打印请求过程的详细信息,帮助定位问题:
import logging import http.client http.client.HTTPConnection.debuglevel = 1 logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True - 拆分测试步骤:先测试网络连通性,再测试不带文件的POST请求,最后添加文件参数,逐步排查
关于通用错误调试方法
谷歌搜索是常用手段,但不是唯一方式,更高效的调试流程包括:
- 查看错误栈:错误信息会明确指出问题发生的位置和原因,比如初始错误直接指向DNS解析失败
- 查阅官方文档:requests库的官方文档有详细的错误处理、参数说明,能快速找到对应问题的解决方案
- 分步测试验证:把复杂请求拆分成多个简单步骤,逐个验证每个环节是否正常
- 查看调试日志:开启日志能获取请求、响应的详细过程,发现隐藏的问题
内容的提问来源于stack exchange,提问作者Devadrita Bose
相关产品推荐
相关产品推荐

