I/O error (0):Error while downloading file from ftps using python 2.7 Unable to download file from FTPS

StackOverflow https://stackoverflow.com/questions/12384888

Question

HI I am trying to download file from FTPS using python 2.7. Below is my code Here I am gating unexceptional IO error. While this code is running on UNIX and Python 2.7. I tried on windows with same code and its working fine. But on unix its not working as excepted.

Not getting where I am doing mistake.

#!/usr/bin/env python

import ftplib  
import os
import datetime
import sys
from ftplib import FTP_TLS

try:
  ftps = FTP_TLS(server)
  ftps.debug(3)
  ftps.connect(host=server,port=portno)
  ftps.auth()    
  ftps.login(username, password)
  ftps.prot_p()    
  ftplogin=True
except Exception, e:
    logger.error(e)

# Change to the proper directory
if ftplogin:
    try:
      ftps.cwd(directory)
      filelist = [] #to store all files
      ftps.retrlines('LIST',filelist.append)    # append to list
      is_file_exist=False       
      if len(filelist)>0 :#do something                   
          is_file_exist = True

      if  is_file_exist :
         print "file exist"
          ##Loop through matching files and download each one individually
          try:
              for filename in ftps.nlst(filematch):
                  local_filename = os.path.join(downloadpath, filename)
                  fhandle = open(local_filename, 'wb')                    
                  logger.info('Getting ' + filename)
                  ftps.retrbinary('RETR ' + filename, fhandle.write)
                  fhandle.close()             #                    
              ftps.quit()    
              logger.info("File download successfull")
          except Exception , e:
              print e
              logger.error(e)
     else :
       logger.info("There is no file for processing")  

 except IOError as eo:
    print "I/O error ({0}):{1}".format(eo.errno,eo.strerror)
 except Exception, e:
   directoryFound=False
   logger.error(e)
   print e
 except :
   print "Unexpected erro:", sys.exc_info()[0]

Note: Please ignore Indent as I am copying the code here and its in proper formatted in .Py file

Code is breaking with below error

Abc.txt*
Abc1.txt*
*get* '220 208.235.248.3 FTP server ready\r\n'
*resp* '220 208.235.248.3 FTP server ready'
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '234 AUTH TLS successful\r\n'
*resp* '234 AUTH TLS successful'
*cmd* 'USER Username'
*put* 'USER Username \r\n'
*get* '331 Password required for Username.\r\n'
*resp* '331 Password required for Username.'
*cmd* 'PASS ********'
*put* 'PASS ********\r\n'
*get* '230 User Username logged in.\r\n'
*resp* '230 User Username logged in.'
*cmd* 'PBSZ 0'
*put* 'PBSZ 0\r\n'
*get* '200 PBSZ 0 successful\r\n'
*resp* '200 PBSZ 0 successful'
*cmd* 'PROT P'
*put* 'PROT P\r\n'
*get* '200 Protection set to Private\r\n'
*resp* '200 Protection set to Private'
2012-09-12 05:01:54,029 - __main__ - INFO - Server login successful
changeing directory
*cmd* 'CWD /prod/hm'
*put* 'CWD /prod/hm\r\n'
*get* '250 CWD command successful.\r\n'
*resp* '250 CWD command successful.'
Directory changed/prod/hm
filelist init
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type set to A\r\n'
*resp* '200 Type set to A'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (208,235,248,3,232,171).\r\n'
*resp* '227 Entering Passive Mode (208,235,248,3,232,171).'
*cmd* 'NLST'
*put* 'NLST\r\n'
*get* '150 Opening ASCII mode data connection for file list\r\n'
*resp* '150 Opening ASCII mode data connection for file list'
*retr* 'Abc.txt\r\n'
*retr* 'Abc1.txt\r\n'
*retr* ''
I/O error (0):Error

I have change the file name and FTPS details for security. :)

I tried with lot of option but there is no workaround. If I change this coed with FTP and make require code changes like Comment ftps.auth(), ftps.prot_p() then it working fine but for FTPS is not working.

I am not getting the error as well to explore There is no information available on Internet as I search lot but no solution Please help 

I am using ftplib over ftp_tls class and python2.7 on unix

Was it helpful?

Solution

Its seems ftplib.py not working properly on UNIX in python2.7 I don't know whether any one getting this error on letest version of python like python 3.2 . In ftplib.py under FTP_TLS class retrbinary and retrlines command having conn.unwrap() method. This method not closing connection and reopen properly on unix. instead of conn.unwrap() if we can use conn.close() it will work. Please do the below change inside ftplib.py

 def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
     ...
     ...
      if isinstance(conn, ssl.SSLSocket):
         ##conn.unwrap()
         conn.close()
     ....

and

 def retrlines(self, cmd, callback = None):
     ....
     ....
         if isinstance(conn, ssl.SSLSocket):
         ##conn.unwrap()
         conn.close()
     .....
         conn.close()

I dont know whether its having any impact on other functionality. Please cross check before implementing this modification on production

OTHER TIPS

The following is a clue, not an answer ... I will update later if we make more progress on an answer.

We have also experienced this error. The clue we found is that the error occurs on Linux machines but not Windows machines. We use the same version of Python (2.7.2) on both machines. This suggests the issue relates to sockets or something that is implemented differently between the two OSs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top