[Tutor] transferring files over a networky
Michael P. Reilly
arcege@speakeasy.net
Wed, 25 Apr 2001 16:14:56 -0400 (EDT)
Stephen Aichele wrote
>
>
> I'm looking for suggestions for implementing P2P filesharing over a network using Python.
>
> with respect to the connection itself, I think I'm ok. However, I'm thinking that there must be a better way to handle the data transfer than what I'm currently doing:
>
> open the file on machine #1, read it into a variable, and send the data (as a python variable) to machine #2. machine #2 then catches it and writes it to a file.
>
> this is all well and good for simple files, but for longer files it seems like too much data to have read into a variable and sent all at once. seems there should be a way to copy the file directly from machine #1 to machine #2.
>
> If anyone has a minute to spare with a suggestion or two, I'd really appreciate it...
Generally, you want to send "blocks" of data across.
>>> blksize = 2048 # 2KB
>>> f = open(filename, 'rb')
>>> conn = get_remote_connection(...)
>>> blk = f.read(blksize)
>>> while blk:
... conn.send(blk)
... blk = f.read(blksize)
...
>>> conn.shutdown(0) # tell the otherwise we're done
>>> f.close()
This way, you send 2 kilobytes chunks of the file to the other side at
a time (you can adjust the size as needed, but try to make it a power
of two). The last block will likely not be the full size, but the data
read in and transmitted will be correct, and the remote side will get
just that much as needed. The receiver is the reverse of the sender
and would look like:
>>> blksize = 2048
>>> f = open(filename, 'wb')
>>> conn = receive_connection()
>>> blk = conn.recv(blksize)
>>> while blk:
... f.write(blk)
... blk = conn.recv(blksize)
...
>>> f.close()
>>> conn.shutdown(0)
(You'll have to fill in the network connection routines as needed.)
Good luck!
-Arcege
--
+----------------------------------+-----------------------------------+
| Michael P. Reilly | arcege@speakeasy.net |