-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_client.py
40 lines (29 loc) · 976 Bytes
/
ftp_client.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
from multiprocessing import Pool
from functools import partial
from ftplib import FTP
from pathlib import Path
import json
def get_config(config_name):
with open(config_name, 'r') as f:
return json.load(f)
def get_ftp_connection(host, port, name, passwd):
ftp = FTP('')
ftp.connect(host, port)
ftp.login(name, passwd)
return ftp
def upload(file, ftp_data):
with open(file[0], 'rb') as f:
with get_ftp_connection(ftp_data['host'], ftp_data['port'], ftp_data['user'], ftp_data['pass']) as ftp:
p = Path(file[0])
try:
ftp.cwd(file[1])
except:
ftp.mkd(file[1])
ftp.cwd(file[1])
ftp.storbinary('STOR %s' % p.name, f)
def main():
config_data = get_config('./config.json')
pool = Pool(config_data['pool_num'])
pool.map(partial(upload, ftp_data=config_data['ftp']), config_data['files'])
if __name__ == "__main__":
main()