我正在尝试使用Python Web服务器响应HTTP GET请求来提供大文件。我已成功覆盖了do_GET(self),一切都适用于发送小型zip文件,但是对于较大的文件,我只能将大约137MB写入wfile输出流,然后才会出现管道错误,因为BaseHTTPServer在我写完之前调用了完成
self.send_header("Content-Type", "application/octet-stream")
self.end_headers()
f = open(filename, 'rb')
fullyWritten = False
count = 0
while not fullyWritten:
tmp = f.read(32768)
count = count + len(tmp)
print count # always a different number before crash
if not tmp:
fullyWritten = True
else:
self.wfile.write(tmp) # never finishes writing large files
print "wrote to http body"
...
...
wrote to http body
137134080
wrote to http body
137166848
wrote to http body
137199616
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 43644)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
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
----------------------------------------
GO代码正在调用终点:
res, err := http.Get(remoteUri)
if err != nil {
return err
}
defer res.Body.Close()
bodyBytesBuffer := new(bytes.Buffer)
_, err = io.Copy(bodyBytesBuffer, res.Body)
if err != nil {
return err
}
urlReader := bytes.NewReader(bodyBytesBuffer.Bytes())
var zipReader *zip.Reader
zipReader, err = zip.NewReader(urlReader, int64(urlReader.Len()))