-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (60 loc) · 2.33 KB
/
Copy pathmain.py
File metadata and controls
78 lines (60 loc) · 2.33 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
__author__ = 'https://github.com/password123456/'
import os
import requests
import hashlib
import importlib
import sys
import time
importlib.reload(sys)
class Bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def download_file(url):
saved_path = '/home/download/'
file_name = '%s/%s' % (saved_path, url.split('/')[-1])
try:
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
'Connection': 'keep-alive'}
with open(file_name, 'wb') as f:
print('%s Download URL: %s %s' % (Bcolors.OKGREEN, url, Bcolors.ENDC))
r = requests.get(url, headers=header, stream=True)
download_file_length = r.headers.get('Content-Length')
print('%s Downloading: %s / %.2f MB %s'
% (Bcolors.OKGREEN, file_name, (float(download_file_length) / (1024.0 * 1024.0)), Bcolors.ENDC))
if download_file_length is None:
f.write(r.content)
else:
dl = 0
total_length = int(download_file_length)
start = time.perf_counter()
for data in r.iter_content(chunk_size=8092):
dl += len(data)
f.write(data)
done = int(100 * dl / total_length)
sys.stdout.write('\r [%s%s] %s/%s (%s%%) - %.2f seconds '
% ('>' * done, ' ' * (100 - done), total_length, dl,
done, (time.perf_counter() - start)))
sys.stdout.flush()
f.close()
if os.path.isfile(file_name):
f = open(file_name, 'rb')
download_file_read = f.read()
file_hash = hashlib.sha256(download_file_read).hexdigest()
print('\n')
print('%s SHA-256: %s%s' % (Bcolors.OKGREEN, file_hash, Bcolors.ENDC))
f.close()
except Exception as e:
print('%s[-] Exception::%s%s' % (Bcolors.WARNING, e, Bcolors.ENDC))
else:
r.close()
def main():
url = 'download file url'
download_file(url)
if __name__ == '__main__':
main()