Loading contents behind the scenes

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat May 24 21:45:48 EDT 2008


En Thu, 22 May 2008 14:05:42 -0300, MRAB <google at mrabarnett.plus.com> escribió:
> On May 22, 3:20 pm, s0s... at gmail.com wrote:

>> > > In my case, what I'm doing is sending the return value through a
>> > > socket:
>>
>> > > sock.send(f.read())
>>
>>
> I would go with:
>
> f = file("filename", "rb")
> while True:
>     data = f.read(MAX_BUF_SIZE)
>     if not data:
>         break
>     sock.sendall(data)

Another way is to use the shutil module:

fin = open("filename", "rb")
fout = sock.makefile()
shutil.copyfileobj(fin, fout)

-- 
Gabriel Genellina




More information about the Python-list mailing list