Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #762 (defect)

Opened 7 months ago

Last modified 5 months ago

Issue serving files to download using cherrypy

Status: new

Reported by: ruifsp@gmail.com Assigned to: Rui
Priority: high Milestone: 2.2.2
Component: CherryPy code Keywords: Download Issue
Cc:

The following code (retrieved from a tutorial example) was used to test cherrypy serving files to download:

import glob
import os.path

import cherrypy
from cherrypy.lib.cptools import serveFile


class Root:
  def index(self, directory="."):
    html = """<html><body><h2>Here are the files in the selected directory:</h2>
      <a href="index?directory=%s">Up</a><br />
      """ % os.path.dirname(os.path.abspath(directory))
      for filename in glob.glob(directory + '/*'):
        absPath = os.path.abspath(filename)
        if os.path.isdir(absPath):
          html += '<a href="/index?directory=' + absPath + '">' + os.path.basename(filename) + "</a> <br />"
        else:
          html += '<a href="/download/?filepath=' + absPath + '">' + os.path.basename(filename) + "</a> <br />"
               
      html += """</body></html>"""
      return html
  index.exposed = True

class Download:
    
    def index(self, filepath):
        return serve_file(filepath, "application/x-download", "attachment")
    index.exposed = True

if __name__ == '__main__':
    root = Root()
    root.download = Download()
    cherrypy.root = root
    cherrypy.server.start()

When using this code in CP2.2 a file handler is created each time a download is requested by a client. So far so good, the problem arrives if the client cancels or interrupts the download before it ends (tested this using a file with a few MB). If so, the file handle keeps opened as long as the server is running, not allowing other processes to delete the file. This file is locked until the server restarts.

Is this some mistake I'm doing or is it really a CP issue?

Change History

02/04/08 20:52:05: Modified by fumanchu

  • description changed.

02/18/08 13:38:46: Modified by lawouach

I wonder if this is easily doable because I don't think the CP HTTP server can inform the CP engine that the connection was closed, meaning that there is no way we can act upon such event.

Maybe one way would be to have a separate thread that monitors for handles owned by the Python processus and decide whether or not that resource needs to be released.

Hosted by WebFaction

Log in as guest/cpguest to create tickets