This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpcom.py
109 lines (75 loc) · 2.17 KB
/
ftpcom.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
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
#!/usr/bin/env python3
# ftpcom.py
#
# @link https://github.com/ctlcltd/e2-sat-editor-qb
# @copyright e2 SAT Editor Team
# @author Leonardo Laureti
# @version 0.1
# @license MIT License
#
from ftplib import FTP
import time
from commons import *
class FTPcom(FTP):
def __init__(self, *args, **keywords):
FTP.__init__(self, *args, **keywords)
def open(self, host, port, user, passwd):
connect = self.connect(host=host, port=int(port))
login = self.login(user=user, passwd=passwd)
debug('FTPcom', 'open()', self.getwelcome())
if connect.startswith('220') and login.startswith('230'):
return self
else:
self.close()
raise Exception('FTPcom', 'could not connect', [connect, login])
def retrieve(self, source, outfile, close=False, read=False):
debug('FTPcom', 'retrieve()', 'START')
q = queue.Queue()
def retry(start=None):
self.retrbinary('RETR ' + source, callback=q.put, rest=start)
q.put(None)
threading.Thread(target=retry).start()
with open(outfile, 'wb') as output:
while True:
chunk = q.get()
if chunk is not None:
debug('FTPcom', 'retrieve()', 'REST')
output.write(chunk)
else:
debug('FTPcom', 'retrieve()', 'END')
if close:
self.close()
break
if read:
with open(outfile, 'rb') as input:
return input.read()
def retrievechunked(self, source, outfile, retry_delay, close=False, read=False):
debug('FTPcom', 'retrievechunked()', 'START')
q = queue.Queue()
def retry(start=None):
self.retrbinary('RETR ' + source, callback=q.put, rest=start)
q.put(None)
threading.Thread(target=retry).start()
with open(outfile, 'wb') as output:
size = 0
last = 0
while True:
chunk = q.get()
size = output.tell()
if chunk is not None:
output.write(chunk)
elif not size == last:
time.sleep(retry_delay)
debug('FTPcom', 'retrievechunked()', 'REST', size)
retry(size)
last = output.tell()
else:
debug('FTPcom', 'retrievechunked()', 'END', size, last)
if close:
self.close()
break
if read:
with open(outfile, 'rb') as input:
return input.read()
def close(self):
debug('FTPcom', 'close()', self.quit())