-
PYTHON et le FTP
FTPRETTY
from ftpretty import ftpretty
Minimal
f = ftpretty(host, user, pass)
Avancé
secure=True argument switches to an FTP_TLS connection default is False
passive=False disable passive connection, True is the defaultf = ftpretty(host, user, pass, secure=True, passive=False, timeout=10)
Downloader un fichier
En l’enregistrant localement :
f.get('chemin/sur/serveur/fichier.txt', '/chemin/sur/local/fichier.txt')Get a file and write to an open file
myfile = open('/tmp/localcopy/server.txt', 'wb') f.get('someremote/file/on/server.txt', myfile)ou
# Get a file and return contents (in python 3 contents is bytes)
contents = f.get('someremote/file/on/server.txt')# Put a local file to a remote location
# non-existent subdirectories will be created automatically
f.put('/tmp/localcopy/data.txt', 'someremote/file/on/server.txt')ou
# Put a local file into a remote directory, denoted by trailing slash on remote
f.put('/tmp/localcopy/data.txt', 'someremote/dir/')ou
# Put using an open file desciptor
myfile = open('/tmp/localcopy/data.txt', 'r') f.put(myfile, 'someremote/file/on/server.txt')ou
# Put using string data (in python 3 contents should be bytes)
f.put(None, 'someremote/file/greeting.txt', contents='blah blah blah')
ou
# Put a tree on a remote directory (similar to shutil.copytree, without following
# symlinks
f.upload_tree("Local/tree", "/remote/files/server")# Return a list the files in a directory
f.list('someremote/folder') ['a.txt', 'b.txt'] f.list('someremote/folder', extra=True) [{'date': 'Feb 11', 'datetime': datetime.datetime(2014, 2, 11, 2, 3), 'directory': 'd', 'group': '1006', 'items': '3', 'name': 'a.txt', 'owner': '1005', 'perms': 'rwxr-xr-x', 'size': '4096', 'time': '02:03', 'year': '2014'}, {'date': 'Feb 11', 'datetime': datetime.datetime(2014, 2, 11, 2, 35), 'directory': 'd', 'group': '1006', 'items': '3', 'name': 'b.txt', 'owner': '1005', 'perms': 'rwxr-xr-x', 'size': '4096', 'time': '02:35', 'year': '2014'}]Change to remote directory
f.cd('someremote/folder')Delete a remote file
f.delete('someremote/folder/file.txt')Fermer la connexion
f.close()
—
—
—