module filehelper.ftp_transfer

Inheritance diagram of pyquickhelper.filehelper.ftp_transfer

Short summary

module pyquickhelper.filehelper.ftp_transfer

provides some functionalities to upload file to a website

source on GitHub

Classes

class

truncated documentation

CannotCompleteWithoutNewLoginException

raised when a transfer is interrupted by a new login

CannotReturnToFolderException

raised when a transfer is interrupted by an exception and the class cannot return to the original folder

TransferFTP

This class uploads files to a website, if the remote does not exists, it creates it first.

Properties

property

truncated documentation

Site

return the website

Methods

method

truncated documentation

__init__

_check_can_logged

_private_login

logs in

close

Closes the connection.

cwd

Goes to a directory, if it does not exist, creates it (if create is True).

dir

Lists the content of a path.

enumerate_ls

Enumerates the content of a path.

ls

Lists the content of a path.

mkd

Creates a directory.

print_list

Returns the list of files in the current directory the function sends everything to the logging function.

pwd

Returns the pathname of the current directory on the server.

retrieve

Downloads a file.

run_command

Runs a FTP command.

transfer

Transfers a file.

Documentation

provides some functionalities to upload file to a website

source on GitHub

exception pyquickhelper.filehelper.ftp_transfer.CannotCompleteWithoutNewLoginException[source][source]

Bases: Exception

raised when a transfer is interrupted by a new login

source on GitHub

exception pyquickhelper.filehelper.ftp_transfer.CannotReturnToFolderException[source][source]

Bases: Exception

raised when a transfer is interrupted by an exception and the class cannot return to the original folder

source on GitHub

class pyquickhelper.filehelper.ftp_transfer.TransferFTP(site, login, password, ftps='FTP', fLOG=<function noLOG>)[source][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 FTP

ftp = 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.

source on GitHub

Parameters
  • site – website

  • login – login

  • password – password

  • ftps – if 'TLS', use class ftplib.FTP_TLS, if 'FTP', use ftplib.TLS, if 'SFTP', use pysftp

  • fLOG – logging function

source on GitHub

property Site[source]

return the website

source on GitHub

__init__(site, login, password, ftps='FTP', fLOG=<function noLOG>)[source][source]
Parameters
  • site – website

  • login – login

  • password – password

  • ftps – if 'TLS', use class ftplib.FTP_TLS, if 'FTP', use ftplib.TLS, if 'SFTP', use pysftp

  • fLOG – logging function

source on GitHub

_check_can_logged()[source][source]
_private_login()[source][source]

logs in

source on GitHub

close()[source][source]

Closes the connection.

source on GitHub

cwd(path, create=False)[source][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

source on GitHub

dir(path='.')[source][source]

Lists the content of a path.

Parameters

path – path

Returns

list of path

See enumerate_ls

source on GitHub

enumerate_ls(path='.')[source][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'}

source on GitHub

ls(path='.')[source][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()

source on GitHub

mkd(path)[source][source]

Creates a directory.

Parameters

path – path to the directory

Returns

True or False

source on GitHub

print_list()[source][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

source on GitHub

pwd()[source][source]

Returns the pathname of the current directory on the server.

Returns

pathname

source on GitHub

retrieve(fold, name, file=None, debug=False)[source][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

source on GitHub

run_command(command, *args, **kwargs)[source][source]

Runs a FTP command.

Parameters
  • command – command

  • args – list of argument

Returns

output of the command or True for success, False for failure

source on GitHub

transfer(file, to, name, debug=False, blocksize=None, callback=None)[source][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.

source on GitHub