2015-06-03 124 views
1

我想運行一個簡單的腳本FTPS從Linux中上傳文件計劃的基礎上,以一個實例FTPS在Windows Server 2012上運行時,我嘗試和測試在我的桌面(OS X)的劇本,腳本錯誤出:Python和ftplib的FTP上傳錯誤。

上傳文件時出錯:[錯誤54]通過對等連接復位

如果我運行在Linux中,相同的錯誤的腳本,除了104而不是54:

上傳文件時出錯:由對等[錯誤104]連接復位

我上傳的文件不是空的就是8個字節。我已經驗證過,ftps與我的桌面上的其他2個客戶端一起工作。我錯過/忽略了什麼?

#!/usr/bin/env python 
from ftplib import FTP_TLS 
import fnmatch 
import os 
import ssl 
import sys 

server = '192.168.1.2' 
user = 'myUsername' 
passwd = 'myPassword' 

def connect_ftp(): 
    ftp = FTP_TLS(server, user, passwd) 
    ftp.set_pasv(True) 
    ftp.prot_p() 

    return ftp 

def upload_file(ftp_connection, upload_file_path): 
    try: 
     upload_file = open("/tmp/test/" + upload_file_path, 'r') 
     print('Uploading ' + upload_file_path + "...") 
     ftp_connection.storbinary('STOR ' + upload_file_path, upload_file) 
     ftp_connection.quit() 
     ftp_connection.close() 
     upload_file.close() 
     print('Upload finished.') 

    except Exception, e: 
     print("Error uploading file: " + str(e)) 

ftp_conn = connect_ftp() 


for file in os.listdir('/tmp/test'): 
     if fnmatch.fnmatch(file, 'bt_*.txt'): 
      upload_file(ftp_conn, file) 

回答

1

我覺得這個問題只在MS FTP服務器上顯示。所有的 首先在調試

ftp.set_debuglevel(2) 

把我的情況傳遞掛在

'STOR的test.xml \ r \ n'

得到「125數據連接已經打開;轉移起始\ n '

RESP' 125數據連接已打開。轉移開始。「

然後我發現這個suggession http://www.sami-lehtinen.net/blog/python-32-ms-ftps-ssl-tls-lockup-fix 我試過(評論conn.unwrap()在storbinary)它和它的工作! 在我的情況下是線513

 # shutdown ssl layer 
     if _SSLSocket is not None and isinstance(conn, _SSLSocket): 
      pass #conn.unwrap() 

這是東北角很糟糕的黑客,但我無法找到更好的東西。