0

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'
Gundars Mēness
  • 488
  • 4
  • 17
  • You didn't say what version of Python you're using, and the code for the `ftplib` module in the latest version [2.7](http://hg.python.org/cpython/file/2.7/Lib/ftplib.py) doesn't match the stack trace in your question. Anyway, if it's an earlier release then it looks like the `pwd()` function has been modified -- possibly because of a bug which you might be running into. – martineau Jun 05 '12 at 17:10
  • 1
    Well if you look at the linked source code, `pwd()` does a `return parse257(resp)` at the end, not a `return self.getresp()` as shown in the stack trace. Perhaps your `ftplib.py` wasn't updated when you upgraded to Py 2.7.3. If not you could try doing it manually. – martineau Jun 05 '12 at 17:41
  • @martineau I added error I get now after manual update of ftplib.py . Still cant figure out, what is wrong :/ – Gundars Mēness Jun 05 '12 at 18:26
  • Sorry that still doesn't match the code in the link (from the Python documentation) given in my first comment. That code has `self.file = self.sock.makefile('rb')` on line 134 within the `connect()` method. – martineau Jun 05 '12 at 18:37
  • @martineau I am using the latest http://hg.python.org/cpython/raw-file/477508efe4ab/Lib/ftplib.py. It has self.file = self.sock.makefile('rb') on line 134. But it still gives me the same error I begun with (the first error in this post). – Gundars Mēness Jun 05 '12 at 19:13
  • True, but that version of the file has the `return parse257(resp)` at the end of `pwd()`, which is inconsistent with the error shown in the stack trace. The only other thing I can think of is that somehow the code isn't importing the `ftplib.py` in the library. Perhaps you can do something to verify whether that is the case. – martineau Jun 05 '12 at 19:47
  • It has nothing to do with my machine, same error from other servers. – Gundars Mēness Jun 07 '12 at 19:27
  • [socket in Python 2.7.3](http://docs.python.org/library/socket.html) does not have a `makefile()` with `encoding` keyword, but [socket in Python 3.2 does](http://docs.python.org/py3k/library/socket.html). Apparently you're still using a 3.X version of ftplib. – Junuxx Jun 07 '12 at 19:43
  • Any suggestions how to adjust script for that? Some different way to import ftplib in script or ... ? – Gundars Mēness Jun 07 '12 at 20:17
  • @Gundars Mēness: If what Junuxx is saying is correct, it sounds like you're mixing two incompatible versions of Python and the best solution seems like it would be to correct that -- maybe by a fresh install of the version you want to use. If you'd like to be able to use both (not at the same time), then maybe something like the [virtaulenv](http://pypi.python.org/pypi/virtualenv) package would help. – martineau Jun 08 '12 at 00:55

1 Answers1

1

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...

Gerald Spreer
  • 413
  • 4
  • 11