python下载ftp文件

 

  
  
  
  
  1. #!/usr/bin/python  
  2. import ftplib  
  3. host='192.168.1.116' 
  4. dir='/printer' 
  5. file='S28BW-412110613520.pdf' 
  6. username='pub' 
  7. passwd='hoolai' 
  8. def main():  
  9.         f=ftplib.FTP(host)  
  10.         try:  
  11.                 f.set_debuglevel(3)  
  12.                 f.login(user=username,passwd=passwd)  
  13.                 f.dir()  
  14.                 print 'login success' 
  15.         except ftplib.error_perm:  
  16.                 print """ERROR:cannot login""" 
  17.                 f.quit()  
  18.                 return 
  19.         try:  
  20.                 f.cwd(dir)  
  21.                 f.dir()  
  22.         except ftplib.error_perm:  
  23.                 print 'error:cannot to change "%s"'%dir  
  24.                 f.quit()  
  25.                 return 
  26.                 print 'changed to "%s"folder'% dir  
  27.         try:  
  28.                 file_local=open(file,"wb").write  
  29.                 f.retrbinary('PETR %s'%file,file_local,8192)  
  30.                 fil_local.close()  
  31.         except ftplib.error_perm:  
  32.                 print 'error:cannot read file "%s"'%file  
  33.         f.quit()  
  34.         return 
  35. if __name__=='__main__':  
  36.         main()  

 

你可能感兴趣的:(python,ftp)