Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 ftplib and pyftpdlib : REST (restart) command not working as expected when uploading
#1
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 :)
Quote
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#3
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
Quote
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to restart another PC on the local network? Kundan 1 269 Sep-21-2019, 11:31 AM
Last Post: ThomasL
  ftplib os.listdir function peterkl 1 2,001 Jun-07-2019, 08:21 AM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)