我目前正在进行一个Python项目,需要通过Python套接字从客户端向服务器传输文件。
这是我的当前代码,但它并未传输整个文件,总是缺少一些字节或者有额外的字节,具体情况与文件大小有关。
_con和con是通过Python套接字连接的连接包装器。
客户端:
def _sendFile(self, path):
sendfile = open(path, 'rb')
data = sendfile.read()
self._con.sendall(data)
self._con.send(bytes('FIN', 'utf8'))
# Get Acknowledgement
self._con.recv(6)
def _recieveFile(self, path):
# Recieve the file from the client
writefile = open(path, 'wb')
i = 0
while (1):
rec = self.con.recv(1024)
if (rec.endswith(b'FIN')):
break
writefile.write(rec)
self.con.send(b'ACK')