开发者

Returning a file to a WSGI GET request

I'm new to WSGI on python; but have a windows server that's got isapi_wsgi installed on it. I also have a script that handles my GET requests all up and running great. The thing is, someone sends me a request, and I need to return a zip file to the requester. The following code is in my GET handler and it works, but doesn't seem like the right way to return the zipfile:

  # open zip file return it
  fin = open(zOutFilename, "rb")
  start_response( "200 OK", [('Content-Type', 'application/zip')])
  return fin.read()

The thing is, you're returning a 'stream' - which means you lose the filename (the browser simply names it the name of the GET query) and it seems awfully slow.

Is there a bette开发者_如何转开发r way to return a file for download with wsgi then this method?

Thanks in advance


Taken directly from PEP 333:

if 'wsgi.file_wrapper' in environ:
    return environ['wsgi.file_wrapper'](filelike, block_size)
else:
    return iter(lambda: filelike.read(block_size), '')

Also you probably want the Content-Disposition header for providing the file name to the client.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜