Transferring files using sockets

Martin v. Loewis martin at v.loewis.de
Wed Apr 10 13:30:37 EDT 2002


theivo at abv.bg (I.C.) writes:

> I am trying to transfer a file using the socket module, but I'm not
> sure how to do this. Should I split the file into little(1KB) packages
> and then transfer them, or is there a possibility to send the whole
> file?

You can transfer the entire file contents in a single .write or .send
call; the TCP implementation will itself split it into smaller
packets. You still need to have a (byte) string object as an argument
for .write/.send; as a result, you would need to read the entire file
first. If the file is very large, reading it all at once may exhaust
the memory - that's why it might be desirable to split it into chunks.

If you don't want to write the copy loop yourself, you could consider
shutil.copyfileobj.

HTH,
Martin




More information about the Python-list mailing list