You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask项目POST数据至/post_data端点遇WinError 10061连接错误求助

问题修复方案

1. 修正端点路径不一致问题

确保请求URL的路径和后端定义的端点完全匹配:

  • 后端定义的是/post_data,请求时必须使用http://localhost:5000/post_data,注意区分下划线_和短横线-,避免用成/post-data

2. 避免服务启动前发送请求

如果在同一脚本中同时包含服务启动和请求逻辑,必须保证服务完全启动后再发送请求。可以用线程分离两者:

from flask import Flask, request, send_file
import pandas as pd
import threading
import time
import requests

app = Flask(__name__)

# 定义POST端点
@app.route('/post_data', methods=['POST'])
def post_data():
    data = request.get_json()
    # 将数据写入Excel
    df = pd.DataFrame(data)
    excel_path = 'data_output.xlsx'
    df.to_excel(excel_path, index=False)
    # 返回下载响应
    return send_file(excel_path, as_attachment=True)

def run_server():
    app.run(debug=False)  # 关闭debug模式避免重复启动

# 启动服务线程
server_thread = threading.Thread(target=run_server)
server_thread.start()

# 等待服务启动完成
time.sleep(2)

# 发送POST请求
data = [{"name": "John", "age": 30}, {"name": "Jane", "age": 28}]
response = requests.post('http://localhost:5000/post_data', json=data)

# 处理下载文件
if response.status_code == 200:
    with open('downloaded_data.xlsx', 'wb') as f:
        f.write(response.content)
    print("Excel文件下载成功")

3. 验证服务运行状态

  • 先单独启动Flask服务,确认控制台输出Running on http://localhost:5000/
  • 用curl或Postman测试端点,确认服务正常响应:
curl -X POST -H "Content-Type: application/json" -d '[{"name":"John","age":30}]' http://localhost:5000/post_data

4. 排查端口与防火墙问题

若服务启动正常仍报10061错误:

  • 检查本地防火墙是否拦截了5000端口
  • 用netstat -ano | findstr :5000命令查看端口是否被占用,若占用则更换Flask端口(比如app.run(port=5001))

内容的提问来源于stack exchange,提问作者Pavan A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:52:09