module filehelper.ftp_transfer
¶
Short summary¶
module pyquickhelper.filehelper.ftp_transfer
provides some functionalities to upload file to a website
Classes¶
class |
truncated documentation |
---|---|
raised when a transfer is interrupted by a new login |
|
raised when a transfer is interrupted by an exception and the class cannot return to the original folder |
|
This class uploads files to a website, if the remote does not exists, it creates it first. |
Properties¶
property |
truncated documentation |
---|---|
return the website |
Methods¶
method |
truncated documentation |
---|---|
logs in |
|
Closes the connection. |
|
Goes to a directory, if it does not exist, creates it (if create is True). |
|
Lists the content of a path. |
|
Enumerates the content of a path. |
|
Lists the content of a path. |
|
Creates a directory. |
|
Returns the list of files in the current directory the function sends everything to the logging function. |
|
Returns the pathname of the current directory on the server. |
|
Downloads a file. |
|
Runs a FTP command. |
|
Transfers a file. |
Documentation¶
provides some functionalities to upload file to a website
- exception pyquickhelper.filehelper.ftp_transfer.CannotCompleteWithoutNewLoginException[source]¶
Bases:
Exception
raised when a transfer is interrupted by a new login
- exception pyquickhelper.filehelper.ftp_transfer.CannotReturnToFolderException[source]¶
Bases:
Exception
raised when a transfer is interrupted by an exception and the class cannot return to the original folder
- class pyquickhelper.filehelper.ftp_transfer.TransferFTP(site, login, password, ftps='FTP', fLOG=<function noLOG>)[source]¶
Bases:
object
This class uploads files to a website, if the remote does not exists, it creates it first.
Transfer files to webste through FTP
Simple sketch to transfer a list of
files
to a website through FTPftp = TransferFTP('ftp.<website>', alias, password, fLOG=print) issues = [ ] done = [ ] notdone = [ ] for file in files : try : r = ftp.transfer (file, path) if r : done.append( (file, path) ) else : notdone.append ( (file, path) ) except Exception as e : issues.append( (file, e) ) try : ftp.close() except Exception as e : print ("unable to close FTP connection using ftp.close")
The class may access to a server using SFTP protocol but it relies on pysftp and paramiko.
- Parameters:
site – website
login – login
password – password
ftps – if
'TLS'
, use class ftplib.FTP_TLS, if'FTP'
, use ftplib.TLS, if'SFTP'
, use pysftpfLOG – logging function
- __init__(site, login, password, ftps='FTP', fLOG=<function noLOG>)[source]¶
- Parameters:
site – website
login – login
password – password
ftps – if
'TLS'
, use class ftplib.FTP_TLS, if'FTP'
, use ftplib.TLS, if'SFTP'
, use pysftpfLOG – logging function
- cwd(path, create=False)[source]¶
Goes to a directory, if it does not exist, creates it (if create is True).
- Parameters:
path – path to the directory
create – True to create it
- Returns:
True or False
- dir(path='.')[source]¶
Lists the content of a path.
- Parameters:
path – path
- Returns:
list of path
See
enumerate_ls
- enumerate_ls(path='.')[source]¶
Enumerates the content of a path.
- Parameters:
path – path
- Returns:
list of dictionaries
One dictionary:
{'name': 'www', 'type': 'dir', 'unique': 'aaaa', 'unix.uid': '1111', 'unix.mode': '111', 'sizd': '5', 'unix.gid': '000', 'modify': '111111'}
- ls(path='.')[source]¶
Lists the content of a path.
- Parameters:
path – path
- Returns:
list of path
see
enumerate_ls
List files from FTP site
from pyquickhelper.filehelper import TransferFTP ftp = TransferFTP("ftp....", "login", "password") res = ftp.ls("path") for v in res: print(v["name"]) ftp.close()
- mkd(path)[source]¶
Creates a directory.
- Parameters:
path – path to the directory
- Returns:
True or False
- print_list()[source]¶
Returns the list of files in the current directory the function sends everything to the logging function.
- Returns:
output of the command or True for success, False for failure
- retrieve(fold, name, file=None, debug=False)[source]¶
Downloads a file.
- Parameters:
file – file name or stream (binary, BytesIO)
fold – full remote path
name – name of the stream on the website
debug – if True, displays more information
- Returns:
status
- run_command(command, *args, **kwargs)[source]¶
Runs a FTP command.
- Parameters:
command – command
args – list of argument
- Returns:
output of the command or True for success, False for failure
- transfer(file, to, name, debug=False, blocksize=None, callback=None)[source]¶
Transfers a file.
- Parameters:
file – file name or stream (binary, BytesIO)
to – destination (a folder)
name – name of the stream on the website
debug – if True, displays more information
blocksize – see ftplib.FTP.storbinary
callback – see ftplib.FTP.storbinary
- Returns:
status
When an error happens, the original current directory is restored.