FTP upload error with Python and ftplib. -
i'm trying run simple ftps script upload file on scheduled basis linux box ftps instance running on windows server 2012. when try , test script on desktop (os x), script errors out:
error uploading file: [errno 54] connection reset peer
if run script on linux box, same error, except 104 instead of 54:
error uploading file: [errno 104] connection reset peer
the files i'm uploading have either been empty or 8 bytes. i've verified ftps working 2 other clients on desktop. missing / overlooking?
#!/usr/bin/env python 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() file in os.listdir('/tmp/test'): if fnmatch.fnmatch(file, 'bt_*.txt'): upload_file(ftp_conn, file)
i think problem shows on ms ftp server. first of turn on debug
ftp.set_debuglevel(2) in case transfer hangs on
put 'stor test.xml\r\n'
get '125 data connection open; transfer starting.\n'
resp '125 data connection open; transfer starting.'
then found suggession http://www.sami-lehtinen.net/blog/python-32-ms-ftps-ssl-tls-lockup-fix i've tried (commented conn.unwrap() in storbinary) , worked! in case in line 513
# shutdown ssl layer if _sslsocket not none , isinstance(conn, _sslsocket): pass #conn.unwrap() this obliviously bad hack, couldn't find better.
Comments
Post a Comment