0

I would like to see progress bar while downloading the file from FTP server. My code looks like this but I get a lot of errors and I don't know why It's happening: NOTE: this is just a "test" code and it is supposed to only print the completed percentage of downloaded file.

def download_file():
    ftp = FTP('exampledomain.com')
    ftp.login(user='user', passwd='pass')

    ftp.cwd('/some_dir/')

    filename = 'file.txt'
    ftp.sendcmd("TYPE i")
    totalsize = ftp.size(filename)
    totalsize = round(totalsize / 1024 / 1024, 1)
    ftp.sendcmd("TYPE A")

    dltracker = FtpDownloadTracker(int(totalsize))
    with open(filename, 'wb') as localfile:
        ftp.retrbinary('RETR ' + filename, localfile.write, 1024, dltracker.handle)

    ftp.quit()
    localfile.close()

class FtpDownloadTracker():
    sizeWritten = 0
    totalSize = 0
    lastPercent = 0

    def __init__(self, totalsize):
        self.totalSize = totalsize

    def handle(self, block):
        self.sizeWritten += 1024
        percentComplete = round((self.sizeWritten / self.totalSize) * 100)

        if self.lastPercent != percentComplete:
            self.lastPercent = percentComplete
            print(str(percentComplete) + " percent complete")

and I get these errors:

Traceback (most recent call last):
  File "D:/PythonProjects/PythonFTP/ftpdl.py", line 148, in run
    ftp.retrbinary('RETR ' + filename, localfile.write, 1024, dltracker.handle)
  File "C:\Python\lib\ftplib.py", line 442, in retrbinary
    with self.transfercmd(cmd, rest) as conn:
  File "C:\Python\lib\ftplib.py", line 399, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python\lib\ftplib.py", line 364, in ntransfercmd
    self.sendcmd("REST %s" % rest)
  File "C:\Python\lib\ftplib.py", line 273, in sendcmd
    return self.getresp()
  File "C:\Python\lib\ftplib.py", line 246, in getresp
    raise error_perm(resp)
ftplib.error_perm: 554-REST needs a numeric parameter
554 Restart offset reset to 0

What am I doing wrong? I can notice there is something called "raise error_perm(resp)" Is it something with server permissions or whatever it is?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Qiasm
  • 356
  • 6
  • 15
  • 1
    The fourth argument of `retrbinary` is restart offset. You cannot pass there some method of your custom object. See [Show FTP download progress in Python (ProgressBar)](https://stackoverflow.com/q/51684008/850848). – Martin Prikryl Aug 22 '19 at 09:07
  • Also, there no point calling `ftp.sendcmd("TYPE A")`. – Martin Prikryl Aug 22 '19 at 09:13
  • So I did the `file_write(data)` function but what exactly is the "data" argument. It's not even defined anywhere. When I want to open the file like that: `with open(filename, 'wb') as localfile: ftp.retrbinary("RETR " + filename, self.file_write)` I get TypeError: file_write() takes 1 positional argument but 2 were given. – Qiasm Aug 22 '19 at 09:16
  • `data` is a chunk of data to be written (of size `blocksize`, which you set to `1024` in your call to `retrbinary`, what is not a good idea, as that will slow down the transfer) -- I cannot answer your other problem without a full code. – Martin Prikryl Aug 22 '19 at 09:20
  • Okay, so here is the code: https://hastebin.com/inizahuluq.rb – Qiasm Aug 22 '19 at 09:23
  • It probably should be `def file_write(self, data):` - And then you will have a problem with `localfile`, as that's not available in your `file_write`. You have to make it a member of your class. – Martin Prikryl Aug 22 '19 at 09:23
  • Okay I did it. It's printing something like: 8192, 8192, 3496, 8192, 328 etc. I guess it will do it until it downloads the file? – Qiasm Aug 22 '19 at 09:29
  • Yes, of course. – Martin Prikryl Aug 22 '19 at 09:33
  • Is there a way to set PyQt's progress bar max value from another class? I mean I do all the download thing in DownloadThread(QtCore.QThread) and the progress bar is in the Ui_MainWindow. I can't see any function called "setMaximum" – Qiasm Aug 22 '19 at 09:36
  • That's way beyond the scope of this question. I believe you already have an answer to this question. If my answer to the linked question has helped you, please consider upvoting it (the same for your [first question](https://stackoverflow.com/q/57599698/850848)). And post a new question for your new problem with the progress bar. – Martin Prikryl Aug 22 '19 at 09:41

0 Answers0