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 33 34 35 36 37 38 39 40 41
|
import ftplib
import os
import time
class ftp(ftplib.FTP):
def __init__(self, adresse='xx.xx.xx.xx', port=21, user='xxxx', password='xxxxxxx'):
ftplib.FTP.__init__(self, '')
self.adresse = adresse
self.port = port
self.user = user
self.password = password
def Reconnect(self):
try:
print "connect %s: %s" % (self.adresse, self.port)
self.connect(self.adresse, self.port) ## Recherche FTP
self.login(self.user, self.password) ## Connexion
self.set_pasv(False)
except:
print "Erreur de connection: Tentative dans 10 seconde"
time.sleep(10)
self.Reconnect()
def Command(self, command, *args):
try:
return command(*args)
except:
self.Reconnect()
return command(*args)
session = ftp()
lst = []
session.Command(session.retrlines, 'LIST /dossier/', lst.append)
i = 0
for fich in lst:
i += 1
if not i in [1,2,3]:
tab = fich.split()
session.Command(session.retrbinary, "RETR /dossier/%s"%tab[8] , open('c:/dossier/%s'%tab[8], 'wb').write)
session.close() |
Partager