1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import ftplib
import os
class ftp(ftplib.FTP):
def __init__(self, adresse='xxx.xxx.xxx.xxx', port=21, user='xxxxxx', password='xxxxxxxx'):
ftplib.FTP.__init__(self, '')
self.adresse = adresse
self.port = port
self.user = user
self.password = password
def Reconnect(self):
self.connect(self.adresse, self.port) ## Recherche FTP
self.login(self.user, self.password) ## Connexion
def Command(self, command, *args):
try:
return command(*args)
except:
self.Reconnect()
return command(*args)
session = ftp()
lst = []
session.Command(session.retrlines, 'LIST /', lst.append)
i = 0
for fich in lst:
i += 1
if not i in [1,2,3]:
tab = fich.split()
print fich
session.Command(session.retrbinary, "RETR /%s"%(tab[8]) , open('C:\Documents and Settings\Administrateur\Bureau\essai\%s'%tab[8], 'wb').write)
session.close() |
Partager