[mod_python] req.sendfile() questions

Wouter van Marle wouter at squirrel-systems.com
Mon Jun 20 09:59:11 EDT 2005

On Mon, 2005-06-20 at 08:37 -0400, Jim Gallacher wrote:

> > I'm looking into a way of sending out specific files, and detect whether
> > or not the download was successful.
> > For sending the file I think I can best use req.sendfile(path,[offset,
> > len]). That seems easy enough to implement.
> > 
> > But how to detect whether the download was aborted (if so: at what
> > stage), or whether it has finished successful, etc. What I understand
> > after searching the web, is that req.sendfile does come back: just no
> > documentation on what it comes back with!
> It will raise an IOError exception if the client disconnects. Otherwise 
> it will return the number of bytes sent.

OK, I understand.
So basically I should say something like this (totally untested for

def send_file(req):
	filename = "/path/to/file.zip"
		bytes_sent = req.sendfile(filename)
		success = True
	except IOError:
		# client disconnected!
		success = False

I've already noticed you have to add a line like:
req.headers_out['Content-Disposition'] = 'attachment; filename=file.zip'
to give the browser a file name for saving (and in case of Mozilla at
least forces the saving of an mp3 file instead of playing immediately),
req.content_type = mimetype
to set the correct mime type. 

Another, related problem that I ran into: how to write some data to the
browser window, and then send the file? That didn't work. Or after the
download is finished, to write some data to the browser. It seems I have
to start a new request or so? Changing the content_type does not help!

> req.sendfile is a wrapper around the apache function ap_send_fd(). A 
> quick look at this tells me that nbytes is set to 0 on failure, so there 
> is no way for mod_python to determine the progress if the client 
> disconnects.

That's less important to me; complete or not is the main issue here.
By the way, does the req.sendfile() function support resuming a


> I'll add this to the documentation.
> Jim

More information about the Mod_python mailing list