Python Forum
ftplib and pyftpdlib : REST (restart) command not working as expected when uploading - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: ftplib and pyftpdlib : REST (restart) command not working as expected when uploading (/thread-2292.html)



ftplib and pyftpdlib : REST (restart) command not working as expected when uploading - darellon - Mar-05-2017

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 :)


RE: ftplib and pyftpdlib : REST (restart) command not working as expected when uploading - wavic - Mar-05-2017

Hello! 

I am not aware with the ftp protocol neither the ftplib. A brief check of the documentation tells me that you have to pass a string to the rest not an integer. Print the type of rest_pos to see what ftp_dot_size() returns


RE: ftplib and pyftpdlib : REST (restart) command not working as expected when uploading - darellon - Mar-05-2017

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 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()
storbinary() takes care of the conversion of rest_pos from int to str


RE: ftplib and pyftpdlib : REST (restart) command not working as expected when uploading - wavic - Mar-05-2017

Good to know. What you said have crossed my mind but I was thinking that the module takes care of this since you pass a value to rest.