Question

I have a script that connects to server and makes a local copy of the whole directory. EOFerror occurs after exactly 50 files of any choice have been downloaded.

Can anyone please tell me, what is wrong with the script?

ERROR:

Traceback (most recent call last):
  File "ftp.py", line 37, in <module>
    ftp_walk(ftp)
  File "ftp.py", line 17, in ftp_walk
    currdir = ftp.pwd()[1:]
  File "/usr/lib/python2.7/ftplib.py", line 574, in pwd
    resp = self.sendcmd('PWD')
  File "/usr/lib/python2.7/ftplib.py", line 244, in sendcmd
    return self.getresp()
  File "/usr/lib/python2.7/ftplib.py", line 210, in getresp
    resp = self.getmultiline()
  File "/usr/lib/python2.7/ftplib.py", line 196, in getmultiline
    line = self.getline()
  File "/usr/lib/python2.7/ftplib.py", line 186, in getline
    if not line: raise EOFError
EOFError

SCRIPT:

#!/usr/bin/python

import ftplib
import sys
import os
import datetime

def ftp_walk(ftp):    
    dirs = ftp.nlst()
    for item in (path for path in dirs if path not in ('.', '..')):
        try:
            ftp.cwd(item)
            print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")+' DIR: ', ftp.pwd()
            ftp_walk(ftp)
            ftp.cwd('..')
        except Exception, e:
            currdir = ftp.pwd()[1:]
            if not os.path.exists(currdir): os.makedirs(currdir)
            try:
                with open(currdir+"/"+item, 'wb') as f:

                    def callback(data):
                        f.write(data)

                    ftp.retrbinary('RETR %s' % item, callback)
                    f.close()
                    print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")+' RETR: '+ currdir+"/"+item
            except Exception, e:
                print e


ftp = ftplib.FTP("hhhhhhhhhhhhhh")
ftp.login("aaaaaaaa", "bbbbbbbbbbb")
ftp.sendcmd("TYPE I") #binary mode
ftp.set_pasv(True) # Trying Passive mode
ftp.cwd("public_html/eeeeeeee/rrrrrrrr/images")
ftp_walk(ftp)
ftp.quit()

Edit: After manual update of ftplib for python 2.7:

Traceback (most recent call last):
  File "ftp.py", line 29, in <module>
    ftp = ftplib.FTP("something.com")
  File "/usr/lib/python2.7/ftplib.py", line 114, in __init__
    self.connect(host)
  File "/usr/lib/python2.7/ftplib.py", line 150, in connect
    self.file = self.sock.makefile('r', encoding=self.encoding)
TypeError: makefile() got an unexpected keyword argument 'encoding'
Was it helpful?

Solution

I have tried your script and it works without any problems. I just pulled 233 images from my server using it. Try setting the current dir to ftp.cwd("./public_html/eeeeeeee/rrrrrrrr/images") and see what happens...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top