[mod_python] requests are blocking when using req.sendfile

Graham Dumpleton graham.dumpleton at gmail.com
Tue Sep 23 21:55:58 EDT 2008


2008/9/24 Matt Barnicle <mattb at wageslavery.org>:
>> Are you setting a content length on the response before calling
>> req.sendfile()?
>>
>> How many concurrent requests for these files are you receiving and
>> how
>> long does it take to download a file?
>>
>> Graham
>
> yes, i believe so..  now that you mention it, i haven't verified the
> file size from the code below, i will do that to be sure.  here is
> the code:
>
> file_path = self.conf.run.app_folder + 'public' + download.path
> file_stat = os.stat(file_path)
> file_size = str(file_stat.st_size)
>
> self.req.headers_out['Content-Length'] = file_size
> self.req.headers_out['Content-Disposition'] = 'attachment;
> filename=%s' % os.path.basename(file_path)
>
> bytes_sent = self.req.sendfile(file_path)

BTW, now that you are using mod_python 3.3.1, instead of using
req.sendfile(), you could possibly delegate serving of the static file
back to Apache. From memory this can be done using:

  req.filename = self.conf.run.app_folder + 'public' + download.path
  req.finfo = apache.stat(req.filename, apache.APR_FINFO_MIN)
  return apache.DECLINED

By returning apache.DECLINED you say mod_python handler will not
actually handle it, and by having updated req.filename and
req.fileinfo updated to new file, when it falls through to
default-handler it should serve it as static file.

Doing it this way should bypass prior Apache access control checks.
Thus file doesn't need to be in Apache document tree or anywhere else
that is accessible.

I believe doing it this way also has benefit that Apache will
automatically set various headers that you probably wouldn't be.

Please let us know if this alternate method works.

Graham


More information about the Mod_python mailing list