Upload of binary files

Peter Hansen peter at engcorp.com
Thu Mar 13 18:43:46 EST 2003


Irmen de Jong wrote:
> 
> Peter Hansen wrote:
> >>This won't do. It is not guaranteed that a single read() returns
> >>all of your data.
> >
> >
> > Since when?  I know with sockets you don't get a guarantee that
> > recv() gets all the data, but the docs on read() for a file
> > object say:
> >
> >   read ([size])
> >   Read at most size bytes from the file (less if the read hits EOF before
> >   obtaining size bytes). If the size argument is negative or omitted,
> >   read all data until EOF is reached.
> 
> I *may* indeed be wrong here, but consider this:
> I was thinking.. "from your CGI code you're actually reading from
> a socket, that's wrapped in a file-like object".
> Sockets behave like I said. So I thought: the read() behaves like this too.
> Even more so because there is no such thing as "EOF" on sockets.
> How can the read() know when all data have been read?

As _I_ read (no pun intended) the description in the docs, it would 
seem that the guts of read() will basically go back and block on the
socket, until recv() eventually returns '' which indicates the 
equivalent of EOF (i.e. socket closed).  If that's so, then you
still are guaranteed that read() will get all your data, though it
might have to block to do so.

I haven't tried that though... I've never used makefile() on a 
socket.

> Irmen
> 
> PS a read() on a *true* file would read everything, ofcourse, like
> you pointed out.

That reassures me. Thanks.  :-)

-Peter




More information about the Python-list mailing list