Python - BaseHTTPServer do_GET() - wfile.write(filedata) Broken PIPE Python - BaseHTTPServer do_GET() - wfile.write(filedata)出现Broken PIPE错误。

5

我需要建立一个Python Web服务器,用于返回几个3MB的文件。它使用baseHTTPServer来处理GET请求。如何使用wfile.write()发送一个3MB的文件?

from SocketServer import ThreadingMixIn
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import BaseHTTPServer


class StoreHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    request_queue_size = 100

    def do_GET(self):
        try:

            filepath = os.path.join(os.path.join(os.path.dirname(__file__), "tools"), "tools.zip")
            if not os.path.exists:
                print 'Tool doesnt exist'

            f = open(filepath, 'rb')
            file_data = f.read()
            f.close()

            self.send_header("Content-type", "application/octet-stream")
            self.end_headers()
            self.wfile.write(file_data) 
            self.send_response(200)
        except Exception,e:
            print e
            self.send_response(400)

错误:

----------------------------------------
Exception happened during processing of request from ('192.168.0.6', 41025)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

编辑:
客户端代码:
import requests

headers = {'user-agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FSL 7.0.5.01003)'}
r = requests.get(url, headers=headers, timeout=60)

它们是什么类型的文件?您使用的操作系统是什么?您使用哪个代码来启动服务器?(您使用哪个客户端?浏览器?另一个Python脚本?)当客户端关闭服务器正在尝试写入的套接字时,会发生断开管道 - 可能是客户端在结束之前断开连接了? - Markon
Zip文件,客户端= Windows 7,服务器为Ubuntu。客户端和服务器都是Python脚本,其中客户端使用requests模块,服务器使用basehttpserver来处理请求。 - mbudge
1个回答

3

你距离正确的服务器并不远...

你只是没有按照HTTP协议的命令顺序:第一个命令必须send_response(或send_error),然后是其他可能的标头,接着是end_header和数据。

此外,当不必要时,你在内存中加载了整个文件。你的do_GET方法可以这样写:

def do_GET(self):
    try:

        filepath = os.path.join(os.path.join(os.path.dirname(__file__), "tools"), "tools.zip")
        if not os.path.exists:
            print 'Tool doesnt exist'

        f = open(filepath, 'rb')

        self.send_response(200)
        self.send_header("Content-type", "application/octet-stream")
        self.end_headers()
        while True:
            file_data = f.read(32768) # use an appropriate chunk size
            if file_data is None or len(file_data) == 0:
                break
            self.wfile.write(file_data) 
        f.close()
    except Exception,e:
        print e
        self.send_response(400)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接