file transfer over LAN

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Mar 28 06:01:32 CET 2009


En Thu, 26 Mar 2009 21:52:18 -0300, MRAB <google at mrabarnett.plus.com>  
escribió:
> prakash jp wrote:

>> On generating log file on remote systems(say client), I want to  
>> transfer them to the Network Admins(say Server) Computer. In doing so  
>> all the contents of the log file r not transfered, only part of the  
>> file.

>> f=open(r'C:\\Python25\src\log.txt', 'rb')

(better if all of those \ are doubled)

>> s.send(str(fsize).zfill(8))
>> s.send(f.read())
>
> You might want to try sendall() instead of send(). send() doesn't
> guarantee to send all the data, but instead returns the number of bytes
> that it did actually send. sendall(), on the other hand, will keep
> going until all of the data is sent.

Yes, I'd say this is the OP's problem. Same happens on the receiving side,  
unfortunately there is no recvall() method.
A safer approach would be to use s.makefile and shutil.copyfileobj:

(sender) (untested)

s.send(str(fsize).zfill(8))
sfile = s.makefile("wb")
shutil.copyfileobj(f, sfile)
sfile.close()
s.close()
f.close()

(receiver):

fsize=int(conn.recv(8))
sfile = conn.makefile("rb")
shutil.copyfileobj(sfile, f)
sfile.close()
conn.close()
s.close()
f.close()

http://docs.python.org/library/socket.html#socket.socket.makefile
http://docs.python.org/library/shutil.html#shutil.copyfileobj

-- 
Gabriel Genellina




More information about the Python-list mailing list