您好我正嘗試使用python 2.7從FTPS下載文件。下面是我的代碼在這裏我正在控制普通的IO錯誤。雖然此代碼在UNIX和Python 2.7上運行。我試着用相同的代碼和它的工作正常。但在unix上,它不能像例外那樣工作。I/O錯誤(0):使用python從ftps下載文件時出錯2.7無法從FTPS下載文件
沒有得到我所犯的錯誤。
#!/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]
注:請忽略縮進,因爲我在這裏和複製代碼,它在適當的的.py格式的文件
代碼與下面的錯誤
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
打破我修改了文件名稱和FTPS的安全細節。 :)
我嘗試了很多選擇,但沒有解決方法。如果我改變這與FTP的coed,並要求更改代碼如註釋ftps.auth(),ftps.prot_p(),然後它工作正常,但FTPS不起作用。
我沒有得到錯誤以及探索 沒有信息可在網上爲我搜索很多,但沒有解決 請幫我使用過FTPLIB類ftp_tls和python2.7上
unix
在Windows中工作正常,無需更改任何內容。欲瞭解更多信息,請參閱此 http://bugs.python.org/issue4791 http://bugs.python.org/issue3826 –