DownloadFile.py
2.22 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
from urllib2 import URLError
from urllib2 import HTTPError
import urllib2,os
import random
import time
from wx.lib.pubsub import pub
from threading import Thread
class DownloadFile():
def __init__(self,url,nameFile):
self.url = url
self.nameFile = nameFile
self.downloading = True
def start(self):
file_name = self.nameFile
try:
u = urllib2.urlopen(self.url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
except:
return False
block_sz = 8192
while self.downloading:
buffer = u.read(block_sz)
if not buffer:
self.downloading = False
f.write(buffer)
return True
class DownloadFile2(Thread):
def __init__(self,url,nameFile):
self.url = url
self.nameFile = nameFile
self.downloading = True
Thread.__init__(self)
def stop(self):
self.downloading = False
def run(self):
file_name = self.nameFile
try:
u = urllib2.urlopen(self.url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
except :
wx.CallAfter(pub.sendMessage, "erroDeConexao", msg="Falha na conexao")
self.downloading = False;
return False
file_size_dl = 0
block_sz = 8192
while self.downloading:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
wx.CallAfter(pub.sendMessage, "updateProgress", msg=(file_size_dl * 100. / file_size))
if (file_size_dl * 100. / file_size) >=100:
wx.CallAfter(pub.sendMessage, "downloadSucess", msg="Download realizado")