Search code examples
pythonftpftplibpyftpdlib

ftplib and pyftpdlib : REST (restart) command not working as expected when uploading file to server


I've set up a simple FTP server with pyftpdlib and a client with ftplib. When i let the client script run, it uploads the file correctly as expected.

pyftpdlib Server code:

import logging
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.authorizers import DummyAuthorizer

authorizer = DummyAuthorizer()
authorizer.add_user("test","123","C:\\Users\\Usr\\Desktop\\server_data","elradfmwM")
handler = FTPHandler
handler.authorizer = authorizer
logging.basicConfig(level=logging.DEBUG)
connection = ("localhost", 8080)
ftpd = FTPServer(connection, handler)
ftpd.serve_forever()

ftplib Client code #1:

import ftplib

ftp = ftplib.FTP()
ftp.connect("localhost",8080)
ftp.login("test","123")

block_size = 128
c_dir = "C:\\Users\\Usr\\Desktop\\client_data"
filename = "test.pdf" # ~ 30Mb

ftp.dir()
myfile = open(c_dir + "\\" + filename , "rb")

ftp.storbinary("STOR " + filename, myfile, blocksize=block_size)

ftp.dir()
ftp.close()

Now i wanted to test the REST (restart upload/download from a specific position) functionality. So i interrupted the client code while still uploading (simply by closing my command prompt while it was uploading). Next, while the server was still running, i ran the following client code in an attempt to resume the upload from the interrupted position:

ftplib Client code #2:

import ftplib

ftp = ftplib.FTP()
ftp.connect("localhost",8080)
ftp.login("test","123")

block_size = 128
c_dir = "C:\\Users\\Usr\\Desktop\\client_data"
filename = "test.pdf" # ~ 30Mb

ftp.dir()
myfile = open(c_dir + "\\" + filename , "rb")

ftp.voidcmd('TYPE I')
rest_pos = ftp.size(filename)
print(rest_pos)
ftp.storbinary("STOR " + filename, myfile, blocksize=block_size, rest=rest_pos)

ftp.dir()
ftp.close()

When i run client code #2 it does upload but it seems it doesn't start at the correct position.

The file size is ca. 30 Mb

Client code #1 uploads correctly (ca 30Mb)

Client code #2 uploads but the file is larger and corrupted (ca 35Mb)

I've compared the output of rest_pos with the file size under Windows after interrupting and they do match. So the position rest i'm passing to ftp.storbinary is the same as under Windows.

I'm fairly new to python and ftp and cant figure out what the issue is. Googled, but couldn't find anything similar.

Any tips/hints would be appreciated :)


Solution

  • I guess i made a silly mistake. In case someone else encounters the same, i had to add myfile.seek(rest_pos,0) in Client code # 2 to start reading the file to be sent at the specific position. So it should look like this:

    import ftplib
    
    ftp = ftplib.FTP()
    ftp.connect("localhost",8080)
    ftp.login("test","123")
    
    block_size = 128
    c_dir = "C:\\Users\\Usr\\Desktop\\client_data"
    filename = "test.pdf" # ~ 30Mb
    
    ftp.dir()
    myfile = open(c_dir + "\\" + filename , "rb")
    
    ftp.voidcmd('TYPE I')
    rest_pos = ftp.size(filename)
    print(rest_pos)
    
    myfile.seek(rest_pos,0)
    
    ftp.storbinary("STOR " + filename, myfile, blocksize=block_size, rest=rest_pos)
    
    ftp.dir()
    ftp.close()