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 42
|
# -*- coding: utf-8 -*-
import os
import ftplib
class FtpUploadTracker:
def __init__(self, totalSize):
self.totalSize = totalSize
self.sizeWritten = 0
self.RefValue=0
print '[',
def handle(self, block):
self.sizeWritten += len(block)
if self.sizeWritten < self.totalSize:
if round((float(self.sizeWritten)/float(self.totalSize))*100) - self.RefValue==1:
print str(int((float(self.sizeWritten)/float(self.totalSize))*100))+'%',
self.RefValue=self.RefValue+5
else:
print '100%] upload terminé.'
# Connect
ftp = ftplib.FTP('hôte', 'login', 'pass')
# file to send
fichier = 'test.zip'
print 'File Name: '+fichier
totalSize = os.path.getsize(fichier)
print 'Size of file: '+str(totalSize/(1024*1024))+'Mo'
f = open(fichier, 'rb')
uploadTracker = FtpUploadTracker(totalSize)
toto=open('sizeWrittenOutput.out','w')
# Send the file
ftp.storbinary('STOR ' + fichier, f, 1024, uploadTracker.handle)
# Close file and FTP
f.close()
ftp.quit()
toto.close() |
Partager