这是我在这里的第一篇文章,所以我很乐意成为社区的一部分。我有一个相当普通的问题要问,但这是一个相当烦人的问题,所以我希望找到答案。使用Python的FTP库检索文件
所以我想用Python的FTPLIB模块来检索一个二进制文件。
代码直接输入到解释是这样的:
>>> from ftplib import FTP
>>> ftp = FTP('xxx.xx.xx.x') # IP of target device
>>> ftp.login()
>>> file = "foobar.xyz" # target file
>>> ftp.retrbinary("RETR " + file, open('filez.txt', 'wb').write)
虽然某些功能的工作(我可以查看设备上的所有文件,您可以通过FTP服务器应用程序的欢迎消息,甚至重命名文件),当我尝试执行上面的最后一个命令,我得到
error_perm Traceback (most recent call last)
/Users/user_name/<ipython console> in <module>()
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in retrlines(self, cmd, callback)
419 if callback is None: callback = print_line
420 resp = self.sendcmd('TYPE A')
--> 421 conn = self.transfercmd(cmd)
422 fp = conn.makefile('rb')
423 while 1:
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in transfercmd(self, cmd, rest)
358 def transfercmd(self, cmd, rest=None):
359 """Like ntransfercmd() but returns only the socket."""
--> 360 return self.ntransfercmd(cmd, rest)[0]
361
362 def login(self, user = '', passwd = '', acct = ''):
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in ntransfercmd(self, cmd, rest)
327 if rest is not None:
328 self.sendcmd("REST %s" % rest)
--> 329 resp = self.sendcmd(cmd)
330 # Some servers apparently send a 200 reply to
331 # a LIST or STOR command, before the 150 reply
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in sendcmd(self, cmd)
241 '''Send a command and return the response.'''
242 self.putcmd(cmd)
--> 243 return self.getresp()
244
245 def voidcmd(self, cmd):
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.pyc in getresp(self)
216 raise error_temp, resp
217 if c == '5':
--> 218 raise error_perm, resp
219 raise error_proto, resp
220
error_perm: 502 Command not implemented.
我已经通过FTPLIB源看了,但我对这些各种各样的任务的编程经验是相当有限的,因为我通常使用Python的数学并没有必须工作无线之前的FTP。
所以,如果有人能给我一些解决方案的想法,这将是一个巨大的帮助。或者,如果您可以使用另一种语言提供另一种解决方案路径,这同样有帮助。
或者您可以使用wget :) – 2010-12-04 20:00:19