文件传输,文件传输后无法打开

时间:2016-05-20 06:38:22

标签: python file sockets tcp

我目前正在处理文件传输服务器并遇到了 一个问题。我能够完整地传输文件并且它完美地工作, 但是当收到文件的客户端无法通过python打开它时。

我的意思是,如果我转移文件,我可以在收到它的客户端的序列中看到它,但它无法打开它,我得到一个:IOError,该文件不存在。< / p>

服务器:

def download_manager(self, sock, ADDR, name):
    sock.send('Starting file download: '+name)
    # Getting the socket that has the file
    # Getting the user ip address
    BUFSIZ = 1024
    fileSock = socket(AF_INET, SOCK_STREAM)
    fileSock.connect(ADDR)
    print 'connected;'
    # Starting the file request protocol.
    tries = "2"
    # sending number of retries.
    fileSock.send(tries + "," + name)
    sock.send(tries)
    for i in range(int(tries)):
        # Accepting the start message.
        data, size = fileSock.recv(BUFSIZ).split(',')
        sock.send(size)
        size = int(size)
        if data == 'Start':
            fileSock.send('ok')
            # Getting first data from the supplier.
            data = fileSock.recv(BUFSIZ)
            # Sending the data to the request client.
            sock.send(data)
            current_size = BUFSIZ
            while current_size <= size:
                # Getting data from the supplier.
                data = fileSock.recv(BUFSIZ)
                # The server "sleeps" in order to keep a synchronization between the client and the server.
                # The client works slower than the server(It needs to save the file as well.
                time.sleep(0.0001)
                # Sending the data to the request client.
                sock.send(data)
                current_size += BUFSIZ
            print current_size
            # Receive for the request client the end message.
            #sock.send(data)
            data = sock.recv(1024)
            if data == 'ok':
                fileSock.send(data)
                break
            else:
                fileSock.send(data)
        else:
            sock.send("wrong")

    fileSock.close()

发件人客户,只是相关部分:

            print "connection from:", addr
            # From here on, the server works by the File-Transfer RFC.
            # Accepting number of retries from the server and the name of the file.
            data = clientsock.recv(self.BUFSIZ)
            tries, name = data.split(',')
            # Opening File.
            f = file(name, mode='rb')
            if not tries:
                clientsock.close()
                continue
            try:
                for i in range(int(tries)):
                    # Sending the file size.
                    clientsock.send('Start,'+str(self.file_size(name)))
                    # Accepting an ok.
                    ok = clientsock.recv(self.BUFSIZ)
                    if not ok:
                        continue
                    if ok == "ok":
                        # Sending the file.
                        clientsock.send(f.read(-1))
                        # Waiting for confirmation.
                        ok = clientsock.recv(self.BUFSIZ)
                        if ok == "ok":
                            break
                        else:
                            continue
                f.close()
            except IOError as e:
                f.close()
                print e
                # An error has occurred, closing the socket.
                #clientsock.send('End,None')
            clientsock.close()

接收客户:

def get_file(self, name):
    """
    This function receives and saves the requested file from the server.
    :param name: The name of the file( How it is saved )
    :return: None.
    """
    name = name.split('.')
    tries = int(self.sock.recv(self.BUFSIZ))
    progress = 0
    for i in range(tries):
        f = file(name[0] + '.' + name[1], mode='wb')
        final_size = int(self.sock.recv(self.BUFSIZ))
        data = self.sock.recv(self.BUFSIZ)
        f.write(data)
        current_size = self.BUFSIZ
        while current_size <= final_size:
            progress = (float(current_size)/final_size)
            if progress > 0.01:
                self.app.progress = progress
            data = self.sock.recv(self.BUFSIZ)
            f.write(data)
            current_size += self.BUFSIZ
        f.close()
        print "Current: " + str(current_size)
        print "real: " + str(self.file_size(name[0] + '.' + name[1]))
        print "Wanted: " + str(final_size)
        self.app.progress = None
        if self.file_size(name[0] + '.' + name[1]) == final_size:
            print 'ok'
            self.sock.send('ok')
            break
        else:
            print 'Bad'
            os.remove(name[0] + '.' + name[1])
            self.sock.send('Bad')
            continue

欢迎任何帮助!

0 个答案:

没有答案