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

自制HTTP服务器向移动Chrome传大文件时出现broken pipe错误

移动Chrome传输大文件时出现Broken Pipe错误的Web服务器问题

我编写的Web服务器处理简单Web服务任务时运行正常,但在向移动Chrome设备传输大文件时,会出现“broken pipe”错误。经确认,移动Chrome与其他浏览器的唯一区别是其请求头中未携带“Keep-alive”字段。目前尚未尝试任何解决办法,期望该服务器能像在其他浏览器中一样实现成功连接。

以下是我的代码:

import socket
import os
import mimetypes

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind(('0.0.0.0', 80))
    s.listen()
    while True:
        conn, addr = s.accept()
        with conn:
            req = conn.recv(1024).decode("utf-8")
            print(req)
            req = req.split(" ")
            if(req[0] == "GET"):
                try:
                    fname = req[1]
                    if(fname == "/"):
                        fname = "index.html"
                    else:
                        fname = os.path.relpath(os.path.abspath("." + fname))
                    with open(fname, "rb") as f:
                        mtype = mimetypes.guess_type(fname)[0]
                        conn.sendall(f"HTTP/1.1 200 OK\nContent-Type: {mtype}\n\n".encode('utf-8') + f.read())
                except:
                    conn.sendall(b"HTTP/1.1 404 Not Found")
            else:
                print("LOL No!")
                conn.sendall(b"HTTP/1.1 400 Bad Request\n\n")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 22:03:19