This repository was archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFtpUploader.py
More file actions
executable file
·93 lines (83 loc) · 3.15 KB
/
FtpUploader.py
File metadata and controls
executable file
·93 lines (83 loc) · 3.15 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
88
89
90
91
92
93
import ftplib
import os
import time
import sys
class FtpUploader:
"""
Allows to upload to a FTP account
"""
def __init__(self, host, username, passwordFile, port=21, logging=None):
if logging:
self.logging = logging
#Retrieve password
with open(passwordFile) as fp:
password = fp.readline().strip()
#Enstablish connection
self.connection = ftplib.FTP_TLS()
self.connection.connect(host, port)
if self.logging:
self.logging.info("FtpUploader - uploadFile - Connected to ["+host+"]")
self.connection.login(username, password)
if self.logging:
self.logging.info("FtpUploader - uploadFile - Logged as ["+username+"]")
self.connection.prot_p()
if self.logging:
self.logging.info("FtpUploader - uploadFile - Connection secured")
self.writtenSize = 0
self.maxSpeed = 0
self.uploadStart = None
def addLogging(self, logging):
"""
Assign a logging handler
"""
self.logging = logging
if logging:
self.logging.info("FtpUploader - Assigned logging handler")
else:
print("No logging handler given")
def uploadFile(self, file_path, block_size=262144, max_speed=0):
"""
Upload a file - Return the upload status
block_size The size of the block to send - Default 256 KiB
maxSpeed Speed in KB/s to maintain during transfer - Default 0 -> No limit
"""
self.maxSpeed = max_speed * 1024
if not self.maxSpeed:
print("Transferring at maximum speed")
self.uploadStart = time.time()
name = os.path.basename(file_path).encode("utf-8", "ignore").decode("latin-1", "ignore")
with open(file_path, 'rb') as fp:
try:
self.connection.storbinary('STOR ' + name, fp, block_size, self.throttler)
if self.logging:
self.logging.info("FtpUploader - uploadFile - Uploaded [" + name + "] - [" + file_path + "]")
print("FtpUploader - uploadFile - Uploaded [" + name + "] - [" + file_path + "]")
return True
except ftplib.error_perm:
self.logging.warn("FtpUploader - uploadFile - Permanent error during upload [" + name + "] - [" + file_path + "]")
except ftplib.error_temp:
self.logging.warn("FtpUploader - uploadFile - Temporary error during upload [" + name + "] - [" + file_path + "]")
return False
total_length = 0
start_time = time.time()
#This function slow down the transmission during
def throttler(self, buf):
i = 0
sleepTime = 0.1
sleepSeconds = 5
#Do nothing if no limit set to max speed
if not self.maxSpeed:
return
#Get the written bytes
self.writtenSize += sys.getsizeof(buf)
cycles = sleepSeconds/sleepTime
while self.writtenSize / (time.time() - self.uploadStart) > self.maxSpeed:
time.sleep(sleepTime)
i += 1
if not i % cycles:
print("Elapsed: "+str(time.time() - self.uploadStart)+"s, started at "+str(self.uploadStart)+" and written "+str(self.writtenSize/1024/1024)+" MB Sleeping since "+str(sleepSeconds)+" seconds")
self.logging.info("Elapsed: "+str(time.time() - self.uploadStart)+"s, started at "+str(self.uploadStart)+" and written "+str(self.writtenSize/1024/1024)+" MB Sleeping since "+str(sleepSeconds)+" seconds")
#Tries to gracefully exit
def __del__(self):
self.connection.quit()
self.logging.info("FtpUploader - __del__ - Quit gracefully")