自制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
相关产品推荐
相关产品推荐

