This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
forked from ut7/gnancraft-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmftp.py
executable file
·122 lines (82 loc) · 2.4 KB
/
mftp.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
from ftplib import FTP, FTP_TLS
import time as time
import os
import re
WDIR = os.environ['FTP_WDIR']
USR = os.environ['FTP_USER']
PWD = os.environ['FTP_PASS']
HST = os.environ['FTP_HOST']
session = None
def getSession():
global session
if session is None :
print("\tFTP Connect")
try :
session = FTP(HST)
session.login(USR,PWD)
except :
print("\tError connecting FTP")
return session
def list_files():
print("\tFTP list files")
ses = getSession()
if ses is None :
return {}
content = []
try :
ses.retrlines('MLSD %s'%(WDIR),content.append)
except :
print("\tError loading directories")
return {}
description = []
for l in content :
d = {}
for e in l.split(';'):
el = e.split('=')
if len(el) == 2:
d[el[0].strip()] = el[1].strip()
elif len(el) == 1:
d['name'] = el[0].strip()
description.append(d)
directories = [d['name'] for d in description if d['type'] == 'dir']
all_files = {}
for d in directories :
l_files = []
try :
ses.retrlines('NLST %s%s/'%(WDIR,d), l_files.append)
except :
print("\tError loading directory")
if len(l_files) > 0 :
all_files[d] = [re.findall('.*?(\w+\.js)$',f)[0] for f in l_files if len(re.findall('.*?(\w+\.js)$',f)) > 0]
return all_files
def save_file(playername,filename,txt) :
path = "%s%s/%s"%(WDIR,playername,filename)
print("\tFTP save %s"%(path))
ses = getSession()
if ses is None :
return
try :
ses.mkd(WDIR+playername)
except:
print('\tFTP dir %s already exists'%(playername))
try:
with open('tempfile', 'wb') as f : f.write(txt.encode('utf8'))
ses.storbinary('STOR %s'%(path), open('tempfile','rb'))
except:
print("\tFTP: ERROR SAVING FILE %s"%path)
return
def load_file(playername,filename):
path = "%s%s/%s"%(WDIR,playername,filename)
print("\tFTP load %s"%(path))
ses = getSession()
if ses is None :
return ""
data = []
try:
res = ses.retrbinary('RETR %s'%(path), data.append)
except:
print("\tFTP ERROR LOADIND FILE %s"%path)
data = [""]
txt = ''.join(data).encode('utf8')
return txt