#!/usr/bin/python import ftplib import os import socket HOST = '192.168.211.120' DIRN = '/var/ftp/pub' FILE = '1' def main(): try: f = ftplib.FTP(HOST) except (socket.error,socket.gaierror),e: print 'ERROR:cannot reach "%s"' % HOST return print '***Connected to host "%s"' % HOST try: f.login() except ftplib.error_perm: print 'ERROR:cannot login anonymously' f.quit() return print '***Logged in as "anonymously"' try: f.cwd(DIRN) except ftplib.error_perm: print 'ERROR:cannot CD to "%s" ' % DIRN f.quit() return print '***Changed to "%s" folder' % DIRN try: f.retrbinary('RETR %s' % FILE,open(FILE,'wb').write) except ftplib.error_perm: print 'ERROR:cannot read file "%s"' % FILE os.unlink(FILE) else: print '***Downloaded "%s" to CWD' % FILE f.quit() return if __name__ == '__main__': main()
C:\Python27\python.exe D:/272343/Desktop/getLatestFTP.py
***Connected to host "192.168.211.120"
***Logged in as "anonymously"
ERROR:cannot CD to "/var/ftp/pub"
Process finished with exit code 0
本文转自aaa超超aaa 51CTO博客,原文链接:http://blog.51cto.com/10983441/1769339