Upload of binary files

Peter Mott pmott at comp.leeds.ac.uk
Thu Mar 13 08:20:44 EST 2003


OK - I've changed the code so it is now:

#! C:/python22/python.exe
import cgi
import cgitb; cgitb.enable()

form = cgi.FieldStorage()

if form.has_key("Data"):
    fileinfo=form["Data"]
    read_in=0
    iteration=0
    while 1:
        chunk = fileinfo.file.read()
        if len(chunk) == 0:
            break
        read_in += len(chunk)
        iteration += 1


print "Content-type: text/plain\n\n"
print "Bytes read:",read_in
print "Read iterations",iteration

With my Apache httpd.conf file I get:
 Bytes read: 36175
 Read iterations 1

With a 3K gra.png file I get:
        Bytes read: 4
        Read iterations 1

With a 66K JPEG file I get:
        Bytes read: 164
        Read iterations 1

So plainly it is _not_ a question of chunked reads. The data is not being
delivered.
This experiment was with XP professional rather than W2K on a different
machine. Python 2.3a2 behaves in the same way. Using Netscape 7 instead of
IE6 makes no difference.

I tried just using standard input:

import sys

read_in=0
iteration=0
while 1:
    chunk = sys.stdin.read()
    if len(chunk) == 0:
        break
    read_in += len(chunk)
    iteration += 1


print "Content-type: text/plain\n\n"
print "Bytes read:",read_in
print "Read iterations",iteration

but the behaviour is exactly the same. The only thing that I have not tried
is using a 'real' network rather than looping back with 127.0.0.1 which is
about all I can think of.

Running out of ideas ..

Peter





"Irmen de Jong" <irmen at -NOSPAM-REMOVETHIS-xs4all.nl> wrote in message
news:3e7050b4$0$49113$e4fe514c at news.xs4all.nl...
> Peter Mott wrote:
> > if form.has_key("Data"):
> >     fileinfo=form["Data"]
> >     data = fileinfo.file.read()
>
> This won't do. It is not guaranteed that a single read() returns
> all of your data. Rather, it is likely to return a chunk at a
> time. So what you probably have to do is extract the Content-Length
> and loop over a read() (appending the results) until you've
> got a result that is the correct length.
>
> Irmen.
>






More information about the Python-list mailing list