2012-08-23 59 views
0

我試圖使用python 2.7從FTP下載文件。在Windows XP無法使用python從FTP下載文件。 [Errno 10054]一個現有的連接被遠程主機強制關閉

我能夠連接FTP但得到以下錯誤

[錯誤10054]現有的連接被強行遠程主機

下面是我的代碼關閉。

import os 
from time import strftime 
from ftplib import FTP 

import smtplib 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEBase import MIMEBase 
from email.MIMEText import MIMEText 
from email import Encoders 


def ftp_connect(path): 
    link = FTP(host = 'myservername', timeout = 5) #Keep low timeout 
    link.login(passwd = 'mypassword', user = 'myusername') 
    debug("%s - Connected to FTP" % strftime("%d-%m-%Y %H.%M")) 
    link.cwd(path) 
    return link 

def writeline(line): 
    file.write(line + "\n") 


downloaded = open('myfile.txtx', 'wb') 

def debug(txt): 
    print txt 

path="mydir" 
filename="myfilename" 

link = ftp_connect(path) 
file_size = link.size(filename) 

max_attempts = 5 #I dont want death loops. 

while file_size != downloaded.tell(): 
    try: 
     debug("%s while > try, run retrbinary\n" % strftime("%d-%m-%Y %H.%M")) 
     if downloaded.tell() != 0: 
      link.retrbinary('RETR ' + filename, downloaded.write, downloaded.tell()) 
     else: 
      link.retrbinary('RETR ' + filename, downloaded.write) 
    except Exception as myerror: 
     if max_attempts != 0: 
      debug("%s while > except, something going wrong: %s\n \tfile lenght is: %i > %i\n" % 
       (strftime("%d-%m-%Y %H.%M"), myerror, file_size, downloaded.tell()) 
      ) 
      link = ftp_connect(path) 
      max_attempts -= 1 
     else: 
      break 
debug("Done with file, attempt to download m5dsum") 

我單獨測試登錄FTP併成功。 但在執行像retrbinary或retrlist提前獲得上述錯誤

感謝任何命令

回答

0

由於FTPS需要diff實現

使用下面的代碼行解決我的問題

ftps = FTP_TLS(server) 
ftps.debug(3) 
ftps.connect(host=server,port=portno,timeout=60) 
ftps.auth() 
ftps.login(username, password) 
ftps.prot_p() 

首先連接到ftps服務器,然後用auth()和prot_p()登錄

相關問題