Python中Requests与urllib3连接异常求助:目标机器主动拒绝连接
Python POST请求连接被拒绝问题排查
问题代码
import requests import json webhook_url = 'http://127.0.0.1:5000/webhook' #'https://webhook.site/580151cf-1c53-488c-a325-dbc76f12a8c5' data = {'name': 'Mathieu', 'Channel URL': 'blablabla'} r = requests.post(webhook_url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
错误信息
Traceback (most recent call last): File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\connection.py", line 203, in _new_conn sock = connection.create_connection( File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection raise err File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen response = self._make_request( File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\connectionpool.py", line 496, in _make_request conn.request( File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\connection.py", line 395, in request self.endheaders() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1280, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1040, in _send_output self.send(msg) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 980, in send self.connect() File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\connection.py", line 243, in connect self.sock = self._new_conn() File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\connection.py", line 218, in _new_conn raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001F901B551C0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\requests\adapters.py", line 486, in send resp = conn.urlopen( File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen retries = retries.increment( File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\urllib3\util\retry.py", line 515, in increment raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type] urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /webhook (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001F901B551C0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it')) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\User\PycharmProjects\Web\main.py", line 10, in <module> r = requests.post(webhook_url, data=json.dumps(data), headers={'Content-Type': 'application/json'}) File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\requests\api.py", line 115, in post return request("post", url, data=data, json=json, **kwargs) File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\requests\sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\requests\sessions.py", line 703, in send r = adapter.send(request, **kwargs) File "C:\Users\User\PycharmProjects\Web\venv\lib\site-packages\requests\adapters.py", line 519, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /webhook (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001F901B551C0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
排查步骤
- 确认本地Webhook服务状态:你请求的
http://127.0.0.1:5000/webhook是本地服务,必须确保该服务已经启动并正常运行。如果服务未启动,目标端口会主动拒绝连接。可以用curl或Postman直接访问该地址,验证连通性。 - 检查端口监听情况:
- Windows系统:打开命令提示符,执行
netstat -ano | findstr :5000,查看5000端口是否有进程在监听。 - Linux/macOS系统:打开终端,执行
lsof -i :5000或ss -tulpn | grep :5000,确认端口占用情况。
- Windows系统:打开命令提示符,执行
- 验证代码本身正确性:将代码中的
webhook_url切换为你注释的公网测试地址,重新运行代码。如果请求成功,说明代码逻辑没问题,问题出在本地服务端配置。 - 代码优化(可选):requests库支持直接传入
json参数,无需手动序列化,代码更简洁且不易出错:import requests webhook_url = 'http://127.0.0.1:5000/webhook' data = {'name': 'Mathieu', 'Channel URL': 'blablabla'} r = requests.post(webhook_url, json=data)
内容的提问来源于stack exchange,提问作者Mathieu P
相关产品推荐
相关产品推荐

