【发布时间】:2014-06-13 07:16:29
【问题描述】:
我使用以下代码(python)通过 TCP 从客户端向服务器发送一个 .txt 文件(大约 87 kbyte 大小):
客户:
f = open(filename, 'r')
while 1:
data = f.read(1024)
if not data:
data='*Endoffile*!'
con.send('%1024s' %data)
f.close()
break
else:
con.send('%1024s' %data)
服务器:
f = open(filename,'w')
while 1:
data = c.recv(1045)
if data=='%1024s' %'*Endoffile*!':
f.close()
break
else:
f.write(data)
问题是收到的文件总是在同一点被剪切(当我打开它时它总是在同一个词处停止),比完整文件的大小小大约 1.6 KB。 有没有人有什么建议?
【问题讨论】:
-
到底缺少什么?不是读写文件,而是在发送方生成模式并在接收方验证模式。另外,您使用的是哪个 Python 版本?
-
1045是从哪里来的? -
Python 是 2.7.x 的最后一个版本
-
1045 是 python 中 1024 个字符的字节大小
-
.txt 文件的最后一个字不见了。