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 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| import sys
from time import sleep
from threading import Thread
import urllib
import os.path
import wx
FichierURL = "http://www.siteinternet.com/fichier_origine.pdf"
FichierDest = "c:\Documents and Settings\ggamer\Mes documents\fichier_destination.pdf"
def AffichetailleFichier(FichierURL):
fichier = urllib.URLopener().open(FichierURL)
tailleFichier = (fichier.info().getheaders('Content-Length'))
tailleFichier = tailleFichier[0]
return tailleFichier
def AffichePourcentage(stade, taille):
pourcent = int(stade*100/taille)
pourcent = str(pourcent) + " %"
return pourcent
def FormateTailleFichier(taille):
if 0 <= taille <1000 :
texte = str(taille) + " octets"
elif 1000 <= taille < 1000000 :
texte = str(taille/1000) + " Ko"
else :
texte = str(taille/1000000) + " Mo"
return texte
def DownloadFichier():
# Renvoie la taille du fichier
fichier = urllib.URLopener().open(FichierURL)
tailleFichier = (fichier.info().getheaders('Content-Length'))
tailleFichier = int(tailleFichier[0])
print "Taille du fichier : ", FormateTailleFichier(tailleFichier)
# Téléchargement du fichier
print u"je commence le téléchargement..."
urllib.urlretrieve(FichierURL, FichierDest)
print u"J'ai terminé le téléchargement !"
# Affiche la taille du fichier téléchargé
print u"Taille du fichier téléchargé : ", os.path.getsize(FichierDest)
class Abort(Exception):
pass
class Download(Thread):
def __init__(self, FichierURL, FichierDest, progressBar, zoneTexte):
Thread.__init__(self)
self.FichierURL = FichierURL
self.stop = False
self.FichierDest = FichierDest
print u"Le téléchargement commence maintenant !"
self.progressBar = progressBar
self.zoneTexte = zoneTexte
def _hook(self, nb_blocs, taille_bloc, taille_fichier):
print u"Téléchargé=", nb_blocs*taille_bloc, u"/ total=", taille_fichier
if nb_blocs*taille_bloc >= taille_fichier:
print "Le téléchargement est terminé !"
self.zoneTexte.SetLabel(u"100 % | Le téléchargement est terminé.")
raise Abort
if self.stop:
raise Abort
count = int(nb_blocs*taille_bloc)
self.progressBar.SetValue(count)
texteInfo = AffichePourcentage(nb_blocs*taille_bloc, taille_fichier)+ " | "+FormateTailleFichier(nb_blocs*taille_bloc)+" / "+FormateTailleFichier(taille_fichier)
self.zoneTexte.SetLabel(texteInfo)
def run(self):
try:
urllib.urlretrieve(self.FichierURL, self.FichierDest, self._hook)
except Abort, KeyBoardInterrupt:
print 'Aborted ici !'
except:
self.stop = True
raise
def abort(self):
self.stop = True
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
panel = wx.Panel(self)
self.boutonGo = wx.Button(panel, -1, u"Télécharger")
self.Bind(wx.EVT_BUTTON, self.Commencer, self.boutonGo)
self.zoneTexte = wx.StaticText(panel, -1, U"Cliquez sur 'Télécharger' pour commencer le téléchargement.", size=(300, -1))
self.progressBar = wx.Gauge(panel, -1, 50, (0, 0), (350, 25))
self.boutonStop = wx.Button(panel, -1, u"Annuler")
self.Bind(wx.EVT_BUTTON, self.Annuler, self.boutonStop)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.zoneTexte, 0, wx.ALL|wx.EXPAND, 15)
sizer.Add(self.progressBar, 0, wx.LEFT|wx.RIGHT|wx.EXPAND, 15)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.boutonGo, 0, wx.LEFT, 15)
sizer2.Add(self.boutonStop, 0, wx.LEFT, 15)
sizer.Add(sizer2, 0, wx.ALL|wx.ALIGN_RIGHT, 15)
panel.SetSizer(sizer)
sizer.SetSizeHints(self)
panel.Layout()
def Commencer(self, event):
max = int(AffichetailleFichier(FichierURL))
self.progressBar.SetRange(max)
print self.progressBar
self.downloader = Download(FichierURL, FichierDest, self.progressBar, self.zoneTexte)
self.downloader.start()
self.boutonGo.Enable(False)
def Annuler(self, event):
# On vérifie si le thread n'a jamais été lancé avant :
try:
downloadEnCours = self.downloader.isAlive()
except AttributeError :
downloadEnCours = False
if downloadEnCours:
# Si le téléchargement est en cours, on le stoppe :
self.downloader.abort()
self.zoneTexte.SetLabel(u"Vous avez interrompu le téléchargement.")
self.boutonGo.Enable(True)
else:
# Si le téléchargement n'est pas en cours, on ferme la fenêtre
self.Destroy()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, u"Téléchargement")
frame.Show(True)
return True
app = MyApp(True)
app.MainLoop() |
Partager