我已经写在扭曲的Web前端Web服务器,与其他Web服务器接口。 客户端上传文件到我的前端服务器,然后发送文件一起到后端服务器。 我想收到上传的文件,然后将文件发送到后端服务器之前,发送到客户端的即时响应。 这样,客户不必等待得到一个响应之前发生的两个上传。
我想在一个单独的线程开始上传到后端服务器来做到这一点。 问题是,发送到客户端的响应后,我不再能够从访问上传的文件Request
的对象。 下面是一个例子:
class PubDir(Resource):
def render_POST(self, request):
if request.args["t"][0] == 'upload':
thread.start_new_thread(self.upload, (request,))
### Send response to client while the file gets uploaded to the back-end server:
return redirectTo('http://example.com/uploadpage')
def upload(self, request):
postheaders = request.getAllHeaders()
try:
postfile = cgi.FieldStorage(
fp = request.content,
headers = postheaders,
environ = {'REQUEST_METHOD':'POST',
'CONTENT_TYPE': postheaders['content-type'],
}
)
except Exception as e:
print 'something went wrong: ' + str(e)
filename = postfile["file"].filename
file = request.args["file"][0]
#code to upload file to back-end server goes here...
当我尝试,我得到一个错误: I/O operation on closed file
。