使用Python进行TCP文件传输

4

我目前正在进行一个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')

2
不是答案,只是一个建议:在传输内容之前删除“FIN”,并发送文件大小。 - yurymik
2个回答

6

虽然你现在面临的首要问题是未将接收到的最后一块数据写入输出文件,但你还有其他几个问题。

可以通过将if语句更改为以下内容来解决当前的问题:

if (rec.endswith(b'FIN')):
    writefile.write(rec[:-3]) # Ignore the last 3 bytes
    break

你还有其他问题:
  1. 如果文件包含字符FIN,那么大约有1/1024的机会这些字符是读取缓冲区中的最后3个字符,并且您的代码将错误地将其读取为结束标记并过早终止。

  2. 还有2/1024的机会,FIN标记将分成两个对read()的调用,其中recFFI结尾。

这两个问题都是由于TCP是基于流的协议,在用户级别上没有数据包的概念所致。

一个明显的解决方法是在文件传输之前加上固定大小的长度指示,接收方读取这个指示,然后读取正确数量的字节。

像这样:

def _sendFile(self, path):
    sendfile = open(path, 'rb')
    data = sendfile.read()

    self._con.sendall(encode_length(len(data)) # Send the length as a fixed size message
    self._con.sendall(data)


    # Get Acknowledgement
    self._con.recv(1) # Just 1 byte


def _recieveFile(self, path):
    LENGTH_SIZE = 4 # length is a 4 byte int.
    # Recieve the file from the client
    writefile = open(path, 'wb')
    length = decode_length(self.con.read(LENGTH_SIZE) # Read a fixed length integer, 2 or 4 bytes
    while (length):
        rec = self.con.recv(min(1024, length))
        writefile.write(rec)
        length -= sizeof(rec)

    self.con.send(b'A') # single character A to prevent issues with buffering

当然,在发送/接收长度时,您需要注意长度字段内字节的顺序。

0
在上面的接收函数循环中,您检查接收到的数据是否以FIN结尾,如果是,则只需退出循环而不将其写入文件。因此,您将会错过最后一部分。
while (1):
    rec = self.con.recv(1024)
    if (rec.endswith(b'FIN')):
        break
    writefile.write(rec)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接