| 12
 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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 
 | #####################################################
#fonction extern
import os, sys, re, subprocess
import Tix ,time
import tkFileDialog
import threading
import ftplib
 
 
class uploadLauncher_MainClass(Tix.Tk):
    def __init__(self,parent):
        Tix.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
        self.threadcount=0
 
    def initialize(self):    
        self.grid()
        ###########FtpUpload########################
        Sendbutton = Tix.Button(self,text=u"FtpUpload",command=self.FtpUp)
        Sendbutton.grid(column=3,row=2,columnspan=1)
        self.grid_columnconfigure(1,weight=3)
        self.grid_columnconfigure(4,weight=1)
        self.grid_columnconfigure(5,weight=3)
        self.resizable(True,False)
 
    def FtpUp(self):
            """fonction appelle par le bouton qui lance un thread par fichier a uploader"""
            self.sendLst="fichier1","fichier2","fichier3","fichier4"
            self.threadFtp={}
            self.meter={}
            self.cancelbtn={}
            for file in self.sendLst:
                    localfile='d:\\'+file+'.mpg' 
                    distantfile='/data/'+file+'.mpg'
 
                    ### recup de la taille du fichier
                    taille= os.path.getsize(localfile)
 
                    self.threadcount=self.threadcount+1
                    self.threadFtp[self.threadcount]=ftpUpThread(self,self.threadcount,localfile,distantfile,taille)
 
                    #creation de la bar de progression 
                    self.meter[self.threadcount] = Tix.Meter(self, text=os.path.split(localfile)[1], value=0.)
                    self.meter[self.threadcount].grid(row = self.threadcount+2, column =1 ,columnspan=2, padx = 3, pady = 3,sticky ='EW')
 
                    #creation du bouton d interuption de l upload
                    self.cancelbtn[self.threadcount] = Tix.Button(self,text=u"cancel",command=self.threadFtp[self.threadcount].cancel)
                    self.cancelbtn[self.threadcount].grid(row=self.threadcount+2,column=3,columnspan=1)
                    self.threadFtp[self.threadcount].start() 
 
 
 
    def forget(self,threadcount):
                 self.cancelbtn[threadcount].destroy()
                 self.meter[threadcount].destroy() 
 
class ftpUpThread(threading.Thread):
    def __init__(self,parent,threadcount,localfile,distantfile,taille):
        self.threadcount=threadcount
        self.parent=parent
        self.distantfile= distantfile     
        self.localfile= localfile
        self.taille= taille
        self.sizeWritten=0
        threading.Thread.__init__(self)
        self._stopevent = threading.Event( ) 
 
    def callback(self,test):
        """met a jour la bare de progression et coupe la connection si on appuis sur cancel"""
        self.sizeWritten += 8192
        app.meter[self.threadcount].config(value=(float(self.sizeWritten)/float(self.taille)))
        app.meter[self.threadcount].update()
        if self._stopevent.isSet(): 
                self.ftp.abort()
                self.ftp.sendcmd('close')
                print "abandon du transfert" 
 
    def cancel(self):
        """passe le stop event en true et efface les widgets correspondant au thread"""    
        self.parent.forget(self.threadcount)  
        self._stopevent.set( )
 
 
    def run(self):
        ###connexion ftp
        self.ftp = ftplib.FTP( )
        self.ftp.connect('un_serveurftp ', 21)
        self.ftp.login('log', 'pass')
        print self.ftp.getwelcome()
 
        ###creation du rep: 
        print os.path.split(self.distantfile)[0]
        try: self.ftp.mkd(os.path.split(self.distantfile)[0])
        except  Exception, err: print err
        print "command envoyer"
        ###envoie fichier
        file = open(self.localfile, 'rb') # ici, j'ouvre le fichier ftp.py 
 
        try: self.ftp.storbinary('STOR '+self.distantfile, file, 8192 ,self.callback) # ici (ou connect est encore la variable de la connexion), j indique le fichier a envoyer
        except  Exception, err: print err
        file.close() # on ferme le fichier
        self.parent.forget(self.threadcount)
        self.ftp.quit()
 
if __name__ == "__main__":
 
    app = uploadLauncher_MainClass(None,)
    app.title('uploadLauncher')
    app.mainloop() | 
Partager