Python ftplib EOFerror

时间:2012-06-05 16:30:14

标签: python ftp eol ftplib

我有一个连接到服务器的脚本,并制作整个目录的本地副本。 在下载了任何选择的50个文件后发生EOFerror。

有人可以告诉我,这个剧本有什么问题吗?

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

编辑: 手动更新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'

1 个答案:

答案 0 :(得分:1)

我已经尝试过你的脚本,它没有任何问题。我刚从服务器上取出了233张图片。尝试将当前目录设置为ftp.cwd("./public_html/eeeeeeee/rrrrrrrr/images"),看看会发生什么......