-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
355 lines (298 loc) · 14.3 KB
/
Copy pathmain.py
File metadata and controls
355 lines (298 loc) · 14.3 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""
Description: This is a module to transfer multiple text/binary files in and out of mainframe using FTP
Date created: 3/18/2019
Python Version: 3.7.1
Dependencies: Bottle micro web framework 0.12
File system: Linux
"""
__author__ = "Kumar Saraboji"
__version__ = "0.1"
__license__ = "None"
__email__ = "me.kumar.saraboji@gmail.com"
__status__ = "development"
import os
from bottle import TEMPLATE_PATH
from bottle import static_file
from bottle import template, get, route, run, request, redirect
from ftplib import FTP
import os
from threading import Thread # to implement threading
# declare global variable
statuses = {} # to capture stats
anyerror = False
def gettext(machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist, iserror):
"""Function to perform FTP GET on text files"""
global anyerror
if not iserror:
with FTP(machine) as host:
host.login(userid, pswd)
# try catch file name errors
try:
with open(filname, 'w') as tofile:
def getline(line):
tofile.write(line + '\n')
host.retrlines("RETR " + dsn, getline)
except:
isfile_exist = False
iserror = True
anyerror = True
host.quit()
if isfile_exist:
bytees = os.path.getsize(filname) # as FTP.size() didn't work used os module as temp/lazy workaround
if iserror and not ismachine_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "text",
"sourcefile": dsn, "destfile": filname, "status": "failure",
"message": "Hostname/IP address doesn't exist"}
elif iserror and not iscred_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "text",
"sourcefile": dsn, "destfile": filname, "status": "failure",
"message": "Incorrect UserID/password"}
elif iserror and not isfile_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "text",
"sourcefile": dsn, "destfile": filname, "status": "failure",
"message": "Source/destination file doesn't exist"}
else:
kb1 = str(round(bytees / 1024, 2)) + " KB transferred"
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "text",
"sourcefile": dsn, "destfile": filname, "status": "success",
"message": kb1}
return
def getbinary(machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist, iserror):
"""Function to perform FTP GET on binary files"""
global anyerror
if not iserror:
with FTP(machine) as host:
host.login(userid, pswd)
# try catch file name errors
try:
with open(filname, 'wb') as tofile:
host.retrbinary("RETR " + dsn, tofile.write)
except:
isfile_exist = False
iserror = True
anyerror = True
host.quit()
if isfile_exist:
# as FTP.size() didn't work used os module as temp/lazy workaround
bytees = os.path.getsize(filname)
if iserror and not ismachine_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "binary",
"sourcefile": dsn, "destfile": filname, "status": "failure",
"message": "Hostname/IP address doesn't exist"}
elif iserror and not iscred_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "binary",
"sourcefile": dsn, "destfile": filname, "status": "failure",
"message": "Incorrect UserID/password"}
elif iserror and not isfile_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "binary",
"sourcefile": dsn, "destfile": filname, "status": "failure",
"message": "Source/destination file doesn't exist"}
else:
kb1 = str(round(bytees / 1024, 2)) + " KB transferred"
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "receive", "format": "binary",
"sourcefile": dsn, "destfile": filname, "status": "success",
"message": kb1}
return
def puttext(machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist, iserror):
"""Function to perform FTP PUT on text files"""
global anyerror
if not iserror:
with FTP(machine) as host:
host.login(userid, pswd)
# try catch file name errors
try:
with open(filname, 'rb') as fromfile:
host.storlines("STOR " + dsn, fromfile)
except:
isfile_exist = False
iserror = True
anyerror = True
host.quit()
if isfile_exist:
# as FTP.size() didn't work used os module as temp/lazy workaround
bytees = os.path.getsize(filname)
if iserror and not ismachine_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "text",
"sourcefile": filname, "destfile": dsn, "status": "failure",
"message": "Hostname/IP address doesn't exist"}
elif iserror and not iscred_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "text",
"sourcefile": filname, "destfile": dsn, "status": "failure",
"message": "Incorrect UserID/password"}
elif iserror and not isfile_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "text",
"sourcefile": filname, "destfile": dsn, "status": "failure",
"message": "Source/destination file doesn't exist"}
else:
kb1 = str(round(bytees / 1024, 2)) + " KB transferred"
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "text",
"sourcefile": filname, "destfile": dsn, "status": "success",
"message": kb1}
return
def putbinary(machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist, iserror):
"""Function to perform FTP PUT on binary files"""
global anyerror
if not iserror:
with FTP(machine) as host:
host.login(userid, pswd)
# try catch file name errors
try:
with open(filname, 'rb') as fromfile:
host.storbinary("STOR " + dsn, fromfile)
except:
isfile_exist = False
iserror = True
anyerror = True
host.quit()
if isfile_exist:
# as FTP.size() didn't work used os module as temp/lazy workaround
bytees = os.path.getsize(filname)
if iserror and not ismachine_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "binary",
"sourcefile": filname, "destfile": dsn, "status": "failure",
"message": "Hostname/IP address doesn't exist"}
elif iserror and not iscred_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "binary",
"sourcefile": filname, "destfile": dsn, "status": "failure",
"message": "Incorrect UserID/password"}
elif iserror and not isfile_exist:
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "binary",
"sourcefile": filname, "destfile": dsn, "status": "failure",
"message": "Source/destination file doesn't exist"}
else:
kb1 = str(round(bytees / 1024, 2)) + " KB transferred"
statuses[trsfrno] = {"machine": machine, "userid": userid, "pswd": pswd, "verb": "send", "format": "binary",
"sourcefile": filname, "destfile": dsn, "status": "success",
"message": kb1}
return
def main(template_path):
TEMPLATE_PATH.insert(0, template_path)
@get("/static/<filename>")
def serve_static_files(filename):
"""Function to serve CSS and JS files"""
return static_file(filename, root='python-pyzftp/static')
@route("/zftp", method=["GET", "POST"])
def process_form():
"""Function to display main webpage and process the form contents"""
global statuses, anyerror
if not anyerror:
machine = ""
userid = ""
pswd = ""
ftno = 1
checked1 = "checked"
checked2 = ""
checked3 = "checked"
checked4 = ""
dsn = ""
filname = ""
else:
for k1, v1 in statuses.items():
machine = v1["machine"]
userid = v1["userid"]
pswd = v1["pswd"]
ftno = ""
checked1 = ""
checked2 = ""
checked3 = ""
checked4 = ""
dsn = ""
filname = ""
break
if request.method == "GET":
return template("zftpmain", machine=machine, userid=userid, pswd=pswd,
ftno=ftno, checked1=checked1, checked2=checked2, checked3=checked3, checked4=checked4,
dsn=dsn, filname=filname, anyerror=anyerror, statuses=statuses)
elif request.method == "POST":
statuses = {}
anyerror = False
ismachine_exist = True
iscred_exist = True
isfile_exist = True
iserror = False
# get all the name attributes from the submitted form
keylst = list(request.forms.keys())
trnsfr1lst = [key for key in keylst if "trsfrno" in key]
trnsfr2lst = [int(key.replace("trsfrno", "")) for key in trnsfr1lst]
machine = request.forms.get("machine")
userid = request.forms.get("userid")
pswd = request.forms.get("pswd")
# try catch hostname/ip error
try:
host = FTP(machine)
except:
ismachine_exist = False
# try catch credentials error
try:
if ismachine_exist:
host.login(userid, pswd)
except:
iscred_exist = False
host.quit()
if not ismachine_exist or not iscred_exist:
iserror = True
anyerror = True
threads = []
for trsfrno in trnsfr2lst:
ckey1 = "ftpverb-radio" + str(trsfrno)
ckey2 = "ftpformat-radio" + str(trsfrno)
if request.forms.get(ckey1) == "receive":
get = True
else:
get = False
if request.forms.get(ckey2) == "text":
text = True
else:
text = False
dsnkey = "dsn" + str(trsfrno)
filnamekey = "filename" + str(trsfrno)
dsn = request.forms.get(dsnkey)
dsn = dsn.replace("'", "")
dsn = dsn.replace('"', "")
dsn = "'" + dsn + "'"
filname = request.forms.get(filnamekey)
if get:
if text:
# log = f"{trsfrno} transfer starts.."
# print(log)
thread = Thread(target=gettext, args=(
machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist,
iserror))
threads.append(thread)
else:
# log = f"{trsfrno} transfer starts.."
# print(log)
thread = Thread(target=getbinary, args=(
machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist,
iserror))
threads.append(thread)
else:
if text:
# log = f"{trsfrno} transfer starts.."
# print(log)
thread = Thread(target=puttext, args=(
machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist,
iserror))
threads.append(thread)
else:
# log = f"{trsfrno} transfer starts.."
# print(log)
thread = Thread(target=putbinary, args=(
machine, userid, pswd, trsfrno, dsn, filname, ismachine_exist, iscred_exist, isfile_exist,
iserror))
threads.append(thread)
for thread in threads:
thread.daemon = True
thread.start()
for thread in threads:
thread.join()
redirect("/zftp/status")
@route("/zftp/status", method=["GET"])
def showmessages():
"""Function to display status webpage"""
return template("zftpstatus", statuses=statuses)
run(host="localhost", port=9001, debug=True) # uses socket - localhost:9001
return
if __name__ == "__main__":
arg_template_path = 'python-pyzftp/views'
main(arg_template_path)