socket + file i/o question

John weekender_ny at
Sun Aug 7 18:08:46 CEST 2005

I am sending a file on a tcp socket using the following code

			while 1:
				buf =, 4096)
				if not buf: break
				print total, len(buf)

The recieving code looks like

		while 1:
			if recvbytes == filesize:
				print 'Transfer done. Size = %d' % recvbytes
			buf = s.recv(4096)
			if not buf:
				print 'EOF received'
				raise Exception()
			print recvbytes, len(buf)
			os.write(fd, buf)
			recvbytes = recvbytes + len(buf)

My problem is that the first time the client uploads a file
to the server, the code works. But then when the server wants
to download a file to the client, the same code breaks down!

The problem seems to be the socket. The send part sends exactly the
amount of data it reads from the file. But the recv part gets one
more byte than the size of the file?? It seems to me that this
extra one byte is coming inside the send/recv calls. Anyone has
any comments on where this extra one byte is coming from?

Thanks a lot for your help,

More information about the Python-list mailing list