Skip to content

Commit 03e8349

Browse files
authored
Add files via upload
1 parent 609348b commit 03e8349

1 file changed

Lines changed: 63 additions & 18 deletions

File tree

pycatfile.py

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
# FTP Support
4646
ftpssl = True
4747
try:
48-
from ftplib import FTP, FTP_TLS
48+
from ftplib import FTP, FTP_TLS, all_errors
4949
except ImportError:
5050
ftpssl = False
51-
from ftplib import FTP
51+
from ftplib import FTP, all_errors
5252

5353
try:
5454
import ujson as json
@@ -390,7 +390,7 @@ def decode_unicode_escape(value):
390390
__version_date__ = str(__version_date_info__[0]) + "." + str(
391391
__version_date_info__[1]).zfill(2) + "." + str(__version_date_info__[2]).zfill(2)
392392
__revision__ = __version_info__[3]
393-
__revision_id__ = "$Id$"
393+
__revision_id__ = "$Id: 4b73b24d1d9cb1fb5011cf0090b2e853058cd6fe $"
394394
if(__version_info__[4] is not None):
395395
__version_date_plusrc__ = __version_date__ + \
396396
"-" + str(__version_date_info__[4])
@@ -9403,19 +9403,39 @@ def download_file_from_ftp_file(url):
94039403
log.info("Error With URL "+url)
94049404
return False
94059405
ftp.login(urlparts.username, urlparts.password)
9406-
if(urlparts.scheme == "ftps"):
9406+
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
94079407
ftp.prot_p()
9408+
# Try EPSV first, then fall back
9409+
try:
9410+
ftp.sendcmd("EPSV") # request extended passive
9411+
ftp.retrlines("LIST", callback=lambda line: None)
9412+
except all_errors:
9413+
try:
9414+
ftp.set_pasv(True)
9415+
ftp.retrlines("LIST", callback=lambda line: None)
9416+
except all_errors:
9417+
ftp.set_pasv(False)
9418+
ftp.retrlines("LIST", callback=lambda line: None)
94089419
ftpfile = MkTempFile()
94099420
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
9410-
#ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
94119421
ftp.close()
94129422
ftpfile.seek(0, 0)
94139423
return ftpfile
94149424

94159425

9426+
def download_file_from_ftps_file(url):
9427+
return download_file_from_ftp_file(url)
9428+
9429+
94169430
def download_file_from_ftp_string(url):
94179431
ftpfile = download_file_from_ftp_file(url)
9418-
return ftpfile.read()
9432+
ftpout = ftpfile.read()
9433+
ftpfile.close()
9434+
return ftpout
9435+
9436+
9437+
def download_file_from_ftps_string(url):
9438+
return download_file_from_ftp_string(url)
94199439

94209440

94219441
def upload_file_to_ftp_file(ftpfile, url):
@@ -9457,21 +9477,40 @@ def upload_file_to_ftp_file(ftpfile, url):
94579477
log.info("Error With URL "+url)
94589478
return False
94599479
ftp.login(urlparts.username, urlparts.password)
9460-
if(urlparts.scheme == "ftps"):
9480+
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
94619481
ftp.prot_p()
9482+
# Try EPSV first, then fall back
9483+
try:
9484+
ftp.sendcmd("EPSV") # request extended passive
9485+
ftp.retrlines("LIST", callback=lambda line: None)
9486+
except all_errors:
9487+
try:
9488+
ftp.set_pasv(True)
9489+
ftp.retrlines("LIST", callback=lambda line: None)
9490+
except all_errors:
9491+
ftp.set_pasv(False)
9492+
ftp.retrlines("LIST", callback=lambda line: None)
94629493
ftp.storbinary("STOR "+urlparts.path, ftpfile)
94639494
ftp.close()
94649495
ftpfile.seek(0, 0)
94659496
return ftpfile
94669497

94679498

9499+
def upload_file_to_ftps_file(ftpfile, url):
9500+
return upload_file_to_ftp_file(ftpfile, url)
9501+
9502+
94689503
def upload_file_to_ftp_string(ftpstring, url):
94699504
ftpfileo = MkTempFile(ftpstring)
94709505
ftpfile = upload_file_to_ftp_file(ftpfileo, url)
94719506
ftpfileo.close()
94729507
return ftpfile
94739508

94749509

9510+
def upload_file_to_ftps_string(ftpstring, url):
9511+
return upload_file_to_ftp_string(ftpstring, url)
9512+
9513+
94759514
class RawIteratorWrapper:
94769515
def __init__(self, iterator):
94779516
self.iterator = iterator
@@ -9582,7 +9621,9 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__):
95829621

95839622
def download_file_from_http_string(url, headers=geturls_headers_pyfile_python_alt, usehttp=__use_http_lib__):
95849623
httpfile = download_file_from_http_file(url, headers, usehttp)
9585-
return httpfile.read()
9624+
httpout = httpfile.read()
9625+
httpfile.close()
9626+
return httpout
95869627

95879628

95889629
if(haveparamiko):
@@ -9639,7 +9680,9 @@ def download_file_from_sftp_file(url):
96399680
if(haveparamiko):
96409681
def download_file_from_sftp_string(url):
96419682
sftpfile = download_file_from_sftp_file(url)
9642-
return sftpfile.read()
9683+
sftpout = sftpfile.read()
9684+
sftpfile.close()
9685+
return sftpout
96439686
else:
96449687
def download_file_from_sftp_string(url):
96459688
return False
@@ -9755,7 +9798,9 @@ def download_file_from_pysftp_file(url):
97559798
if(havepysftp):
97569799
def download_file_from_pysftp_string(url):
97579800
sftpfile = download_file_from_pysftp_file(url)
9758-
return sftpfile.read()
9801+
sftpout = sftpfile.read()
9802+
sftpfile.close()
9803+
return sftpout
97599804
else:
97609805
def download_file_from_pysftp_string(url):
97619806
return False
@@ -9862,10 +9907,12 @@ def download_file_from_internet_string(url, headers=geturls_headers_pyfile_pytho
98629907
def download_file_from_internet_uncompress_string(url, headers=geturls_headers_pyfile_python_alt, formatspecs=__file_format_dict__):
98639908
fp = download_file_from_internet_string(url)
98649909
fp = UncompressFileAlt(fp, formatspecs)
9865-
fp.seek(0, 0)
98669910
if(not fp):
98679911
return False
9868-
return fp
9912+
fp.seek(0, 0)
9913+
fpout = fp.read()
9914+
fp.close
9915+
return fpout
98699916

98709917

98719918
def upload_file_to_internet_file(ifp, url):
@@ -9887,11 +9934,10 @@ def upload_file_to_internet_file(ifp, url):
98879934
def upload_file_to_internet_compress_file(ifp, url, compression="auto", compressionlevel=None, compressionuselist=compressionlistalt, formatspecs=__file_format_dict__):
98889935
fp = CompressOpenFileAlt(
98899936
fp, compression, compressionlevel, compressionuselist, formatspecs)
9890-
if(not catfileout):
9937+
if(not archivefileout):
98919938
return False
98929939
fp.seek(0, 0)
9893-
upload_file_to_internet_file(fp, outfile)
9894-
return True
9940+
return upload_file_to_internet_file(fp, outfile)
98959941

98969942

98979943
def upload_file_to_internet_string(ifp, url):
@@ -9914,8 +9960,7 @@ def upload_file_to_internet_compress_string(ifp, url, compression="auto", compre
99149960
internetfileo = MkTempFile(ifp)
99159961
fp = CompressOpenFileAlt(
99169962
internetfileo, compression, compressionlevel, compressionuselist, formatspecs)
9917-
if(not catfileout):
9963+
if(not archivefileout):
99189964
return False
99199965
fp.seek(0, 0)
9920-
upload_file_to_internet_file(fp, outfile)
9921-
return True
9966+
return upload_file_to_internet_file(fp, outfile)

0 commit comments

Comments
 (0)