-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·50 lines (39 loc) · 1.59 KB
/
main.py
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
import ftplib
import sys
import os
import time
import math
class FtpUploadTracker:
sizeWritten = 0
totalSize = 0
lastShownPercent = 0
def __init__(self, totalSize):
self.totalSize = totalSize
def handle(self, block):
self.sizeWritten += 1024
percentComplete = round((self.sizeWritten / self.totalSize) * 100)
size1 = self.sizeWritten
total = self.totalSize
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
if (self.lastShownPercent != percentComplete):
self.lastShownPercent = percentComplete
b = str(percentComplete) + "%"
print ("Upload... " + filename + " " + b + " {}/{}".format(convert_size(size1), convert_size(total)), end="\r")
ADDR = "ftp.1fichier.com"
USER = "" #EMAIL
PASS = "" #MDP
filename = sys.argv[1]
size = os.stat(filename).st_size
with open(filename, "rb") as file:
uploadTracker = FtpUploadTracker(size)
session = ftplib.FTP(ADDR,USER,PASS) # file to send
session.storbinary('STOR ' + filename, file, 1024, uploadTracker.handle ) # send the file
file.close() # close file and FTP
session.quit()