4545# FTP Support
4646ftpssl = True
4747try :
48- from ftplib import FTP , FTP_TLS
48+ from ftplib import FTP , FTP_TLS , all_errors
4949except ImportError :
5050 ftpssl = False
51- from ftplib import FTP
51+ from ftplib import FTP , all_errors
5252
5353try :
5454 import ujson as json
@@ -378,7 +378,7 @@ def decode_unicode_escape(value):
378378__version_date__ = str (__version_date_info__ [0 ]) + "." + str (
379379 __version_date_info__ [1 ]).zfill (2 ) + "." + str (__version_date_info__ [2 ]).zfill (2 )
380380__revision__ = __version_info__ [3 ]
381- __revision_id__ = "$Id$"
381+ __revision_id__ = "$Id: 51621a2b361db767bc985f415869721fb7be5e6c $"
382382if (__version_info__ [4 ] is not None ):
383383 __version_date_plusrc__ = __version_date__ + \
384384 "-" + str (__version_date_info__ [4 ])
@@ -9391,19 +9391,39 @@ def download_file_from_ftp_file(url):
93919391 log .info ("Error With URL " + url )
93929392 return False
93939393 ftp .login (urlparts .username , urlparts .password )
9394- if (urlparts .scheme == "ftps" ):
9394+ if (urlparts .scheme == "ftps" or isinstance ( ftp , FTP_TLS ) ):
93959395 ftp .prot_p ()
9396+ # Try EPSV first, then fall back
9397+ try :
9398+ ftp .sendcmd ("EPSV" ) # request extended passive
9399+ ftp .retrlines ("LIST" , callback = lambda line : None )
9400+ except all_errors :
9401+ try :
9402+ ftp .set_pasv (True )
9403+ ftp .retrlines ("LIST" , callback = lambda line : None )
9404+ except all_errors :
9405+ ftp .set_pasv (False )
9406+ ftp .retrlines ("LIST" , callback = lambda line : None )
93969407 ftpfile = MkTempFile ()
93979408 ftp .retrbinary ("RETR " + urlparts .path , ftpfile .write )
9398- #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
93999409 ftp .close ()
94009410 ftpfile .seek (0 , 0 )
94019411 return ftpfile
94029412
94039413
9414+ def download_file_from_ftps_file (url ):
9415+ return download_file_from_ftp_file (url )
9416+
9417+
94049418def download_file_from_ftp_string (url ):
94059419 ftpfile = download_file_from_ftp_file (url )
9406- return ftpfile .read ()
9420+ ftpout = ftpfile .read ()
9421+ ftpfile .close ()
9422+ return ftpout
9423+
9424+
9425+ def download_file_from_ftps_string (url ):
9426+ return download_file_from_ftp_string (url )
94079427
94089428
94099429def upload_file_to_ftp_file (ftpfile , url ):
@@ -9445,21 +9465,40 @@ def upload_file_to_ftp_file(ftpfile, url):
94459465 log .info ("Error With URL " + url )
94469466 return False
94479467 ftp .login (urlparts .username , urlparts .password )
9448- if (urlparts .scheme == "ftps" ):
9468+ if (urlparts .scheme == "ftps" or isinstance ( ftp , FTP_TLS ) ):
94499469 ftp .prot_p ()
9470+ # Try EPSV first, then fall back
9471+ try :
9472+ ftp .sendcmd ("EPSV" ) # request extended passive
9473+ ftp .retrlines ("LIST" , callback = lambda line : None )
9474+ except all_errors :
9475+ try :
9476+ ftp .set_pasv (True )
9477+ ftp .retrlines ("LIST" , callback = lambda line : None )
9478+ except all_errors :
9479+ ftp .set_pasv (False )
9480+ ftp .retrlines ("LIST" , callback = lambda line : None )
94509481 ftp .storbinary ("STOR " + urlparts .path , ftpfile )
94519482 ftp .close ()
94529483 ftpfile .seek (0 , 0 )
94539484 return ftpfile
94549485
94559486
9487+ def upload_file_to_ftps_file (ftpfile , url ):
9488+ return upload_file_to_ftp_file (ftpfile , url )
9489+
9490+
94569491def upload_file_to_ftp_string (ftpstring , url ):
94579492 ftpfileo = MkTempFile (ftpstring )
94589493 ftpfile = upload_file_to_ftp_file (ftpfileo , url )
94599494 ftpfileo .close ()
94609495 return ftpfile
94619496
94629497
9498+ def upload_file_to_ftps_string (ftpstring , url ):
9499+ return upload_file_to_ftp_string (ftpstring , url )
9500+
9501+
94639502class RawIteratorWrapper :
94649503 def __init__ (self , iterator ):
94659504 self .iterator = iterator
@@ -9570,7 +9609,9 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__):
95709609
95719610def download_file_from_http_string (url , headers = geturls_headers_pyfile_python_alt , usehttp = __use_http_lib__ ):
95729611 httpfile = download_file_from_http_file (url , headers , usehttp )
9573- return httpfile .read ()
9612+ httpout = httpfile .read ()
9613+ httpfile .close ()
9614+ return httpout
95749615
95759616
95769617if (haveparamiko ):
@@ -9627,7 +9668,9 @@ def download_file_from_sftp_file(url):
96279668if (haveparamiko ):
96289669 def download_file_from_sftp_string (url ):
96299670 sftpfile = download_file_from_sftp_file (url )
9630- return sftpfile .read ()
9671+ sftpout = sftpfile .read ()
9672+ sftpfile .close ()
9673+ return sftpout
96319674else :
96329675 def download_file_from_sftp_string (url ):
96339676 return False
@@ -9743,7 +9786,9 @@ def download_file_from_pysftp_file(url):
97439786if (havepysftp ):
97449787 def download_file_from_pysftp_string (url ):
97459788 sftpfile = download_file_from_pysftp_file (url )
9746- return sftpfile .read ()
9789+ sftpout = sftpfile .read ()
9790+ sftpfile .close ()
9791+ return sftpout
97479792else :
97489793 def download_file_from_pysftp_string (url ):
97499794 return False
@@ -9850,10 +9895,12 @@ def download_file_from_internet_string(url, headers=geturls_headers_pyfile_pytho
98509895def download_file_from_internet_uncompress_string (url , headers = geturls_headers_pyfile_python_alt , formatspecs = __file_format_dict__ ):
98519896 fp = download_file_from_internet_string (url )
98529897 fp = UncompressFileAlt (fp , formatspecs )
9853- fp .seek (0 , 0 )
98549898 if (not fp ):
98559899 return False
9856- return fp
9900+ fp .seek (0 , 0 )
9901+ fpout = fp .read ()
9902+ fp .close
9903+ return fpout
98579904
98589905
98599906def upload_file_to_internet_file (ifp , url ):
@@ -9878,8 +9925,7 @@ def upload_file_to_internet_compress_file(ifp, url, compression="auto", compress
98789925 if (not archivefileout ):
98799926 return False
98809927 fp .seek (0 , 0 )
9881- upload_file_to_internet_file (fp , outfile )
9882- return True
9928+ return upload_file_to_internet_file (fp , outfile )
98839929
98849930
98859931def upload_file_to_internet_string (ifp , url ):
@@ -9905,5 +9951,4 @@ def upload_file_to_internet_compress_string(ifp, url, compression="auto", compre
99059951 if (not archivefileout ):
99069952 return False
99079953 fp .seek (0 , 0 )
9908- upload_file_to_internet_file (fp , outfile )
9909- return True
9954+ return upload_file_to_internet_file (fp , outfile )
0 commit comments