-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
378 lines (346 loc) · 11.3 KB
/
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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#ON START CLIENT SHOULD BE SUPPLIED WITH SERVER IP AND CONTROL PORT NUMBER
from socket import *
import sys
import os
import shutil
import json
import getpass
import glob
if os.name == 'nt':
os.system('color')
global control_port
class State:
def __init__(self, client, server):
self.cwd = os.getcwd()
self.folder = os.path.basename(self.cwd)
self.control = client
self.server = server
self.glob = False
def rls(state):
state.control.send("ls".encode('ascii'))
dir = state.control.recv(1024).decode('ascii')
try:
dirlist = json.loads(dir)
for key, value in dirlist.items():
if ' ' not in key:
if value == 'f':
print(key, end=' ')
else:
CRED = '\033[91m'
CEND = '\033[0m'
print(CRED + key + CEND, end=' ')
else:
if value == 'f':
print('\'' + key + '\'', end=' ')
else:
CRED = '\033[91m'
CEND = '\033[0m'
print(CRED + '\'' + key + '\'' + CEND, end=' ')
print()
except Exception as e:
print(e)
def lls(state):
dirlist = os.listdir(state.cwd)
for l in dirlist:
if ' ' not in l:
if os.path.isfile(l):
print(l, end=' ')
else:
CRED = '\033[91m'
CEND = '\033[0m'
print(CRED + l + CEND, end=' ')
else:
if os.path.isfile(l):
print('\'' + l + '\'', end=' ')
else:
CRED = '\033[91m'
CEND = '\033[0m'
print(CRED + '\'' + l + '\'' + CEND, end=' ')
print()
def rcd(state):
state.control.send(state.command.encode('ascii'))
print(state.control.recv(1024).decode('ascii'))
def lcd(state):
target = state.command[4:]
try:
os.chdir(target)
state.cwd = os.getcwd()
state.folder = target
print('OK')
except Exception as e:
print(e)
def rpwd(state):
state.control.send(state.command.encode('ascii'))
print(state.control.recv(1024).decode('ascii'))
def lpwd(state):
print(state.cwd)
def data_connection(state):
data_port = int(state.control.recv(1024).decode('ascii'))
state.data = socket(AF_INET, SOCK_STREAM)
state.data.connect((state.server, data_port))
def put(state):
target = state.command[4:]
state.control.send(state.command.encode('ascii'))
response = state.control.recv(8).decode('ascii')
if os.path.isfile(target):
state.control.send("file".encode('ascii'))
put_file(state, target)
elif os.path.isdir(target):
state.control.send("dir".encode('ascii'))
put_dir(state, target)
else:
print('Target does not exist.')
state.control.send('201'.encode('ascii'))
def put_file(state, target):
try:
data_connection(state)
f = open(target, 'rb+')
l = f.read(1024)
while(l):
state.data.send(l)
l = f.read(1024)
print('OK')
except Exception as e:
print(e)
finally:
state.data.close()
def put_dir(state, target):
try:
cwd = os.getcwd()
os.chdir(target)
l = []
dirlist = os.scandir()
for entry in dirlist:
if entry.is_dir():
print('The directory is nested. Operation terminated.')
state.control.send(json.dumps('201').encode('ascii'))
os.chdir(cwd)
return
else:
l.append(entry.name)
state.control.send(json.dumps(l).encode('ascii'))
for target in l:
put_file(state, target)
except Exception as e:
print(e)
finally:
os.chdir(cwd)
def rename(state):
state.control.send(state.command.encode('ascii'))
response = state.control.recv(1024).decode('ascii')
if(response == "file2exist"):
print("File with same name exists. Unsuccessful")
elif(response == "!file1"):
print("File 1 does not exist.")
elif(response == "success"):
print("OK")
def get(state):
state.control.send(state.command.encode('ascii'))
target = state.command[4:]
type = state.control.recv(1024).decode('ascii')
if(type == 'file'):
state.control.send('200'.encode('ascii'))
get_file(state, target)
else:
state.control.send('200'.encode('ascii'))
get_dir(state, target)
def get_file(state, target):
try:
data_connection(state)
f = open(target, 'wb+')
data = state.data.recv(1024)
while data:
f.write(data)
data = state.data.recv(1024)
print('OK')
except Exception as e:
print(e)
finally:
state.data.close()
def get_dir(state, target):
try:
cwd = os.getcwd()
os.mkdir(target)
os.chdir(target)
dir = state.control.recv(2048).decode('ascii')
d = json.loads(dir)
if(d == 'NESTED'):
print('The directory is nested. Operation terminated.')
os.chdir(cwd)
return
print(str(d))
for key, value in d.items():
print('Attempting to receive ' + key + ' of type ' + value)
if value == 'f':
get_file(state, key)
else:
get_dir(state, key)
except Exception as e:
print(e)
finally:
os.chdir(cwd)
def mget(state):
state.control.send(state.command.encode('ascii'))
response = state.control.recv(8).decode('ascii')
if(response == '201'):
print('One or more of the specified target files do not exist.\nNote: Toggle file globbing with \'glob\'')
return
elif(response == '202'):
print('No files matching the wildcard expression were found.\nNote: Toggle file globbing with \'glob\'')
return
elif(response == '200'):
state.control.send('200'.encode('ascii'))
text = state.control.recv(2048).decode('ascii')
l = json.loads(text)
state.control.send('200'.encode('ascii'))
for target in l:
get_file(state, target)
def mput(state):
state.control.send(state.command.encode('ascii'))
response = state.control.recv(8).decode('ascii')
targets = state.command[5:].split(" ")
l = []
if(state.glob == False):
for target in targets:
if(os.path.isfile(target)):
l.append(target)
else:
state.control.send('201'.encode('ascii'))
print('One or more of the specified target files do not exist.\nNote: Toggle file globbing with \'glob\'')
return
else:
for pattern in targets:
list = glob.glob(pattern)
for target in list:
l.append(target)
if(len(l) == 0):
state.control.send('201'.encode('ascii'))
print('No files matching the wildcard expression were found.\nNote: Toggle file globbing with \'glob\'')
return
state.control.send('200'.encode('ascii'))
response = state.control.recv(8).decode('ascii')
state.control.send(json.dumps(l).encode('ascii'))
for target in l:
put_file(state, target)
def rmkdir(state):
state.control.send(state.command.encode('ascii'))
print(state.control.recv(1024).decode('ascii'))
def lmkdir(state):
target = state.command[7:]
try:
os.mkdir(target)
print('OK')
except Exception as e:
print(e)
def rrm(state):
state.control.send(state.command.encode('ascii'))
print(state.control.recv(1024).decode('ascii'))
def lrm(state):
target = state.command[4:]
try:
if os.path.isfile(target):
os.remove(target)
print('OK')
else:
shutil.rmtree(target)
print('OK')
except Exception as e:
print(e)
def rsystem(state):
state.control.send(state.command.encode('ascii'))
print(state.control.recv(1024).decode('ascii'))
def lsystem(state):
print(sys.platform)
def toggle_glob(state):
state.control.send(state.command.encode('ascii'))
if(state.glob == True):
state.glob = False
else:
state.glob = True
print(state.glob)
def authenticate_user(state, control_code):
print('Welcome to FTP')
if control_code == 'new':
server_user = state.control.recv(1024).decode('ascii')
server_client_user = input('Name ({}:{}):'.format(server, server_user))
elif control_code == 'again':
server_client_user = input('User: ')
user_pass = getpass.getpass('Password: ')
state.control.send(server_client_user.encode('ascii'))
state.control.send(str(user_pass).encode('ascii'))
auth_result = state.control.recv(1024).decode('ascii')
if auth_result == 'pass':
return
else:
state.command = input('ftp> ')
while state.command != 'user':
print('No connection')
state.command = input('ftp> ')
authenticate_user(state, 'again')
def bye(state):
state.control.send(state.command.encode('ascii'))
if __name__ == '__main__':
global control_port
server = "127.0.0.1"
#server = sys.argv[1]
control_port = int(sys.argv[1])
#control_port = int(sys.argv[2])
client = socket(AF_INET, SOCK_STREAM)
client.connect((server, control_port))
state = State(client, server)
pam_import = state.control.recv(1024).decode('ascii')
if pam_import == '0':
authenticate_user(state, 'new')
while True:
temp_command = input('ftp> ')
state.command = " ".join(temp_command.split())
#SWITCH BASED ON COMMAND
if(state.command == "bye" or state.command == "close" or state.command == "disconnect" or state.command == "quit"):
bye(state)
break
elif(state.command == "ls"):
rls(state)
elif (state.command == "dir"):
rls(state)
elif(state.command == "!ls"):
lls(state)
elif(state.command == "cdup"):
state.command = "cd .."
rcd(state)
elif(state.command[0:3] == "cd "):
rcd(state)
elif(state.command[0:4] == "!cd "):
lcd(state)
elif(state.command == "pwd"):
rpwd(state)
elif(state.command == "!pwd"):
lpwd(state)
elif(state.command[0:4] == "get "):
get(state)
elif(state.command[0:4] == "put "):
put(state)
elif(state.command[0:6] == "mkdir "):
rmkdir(state)
elif(state.command[0:7] == "!mkdir "):
lmkdir(state)
elif(state.command[0:3] == "rm "):
rrm(state)
elif(state.command[0:4] == "!rm "):
lrm(state)
elif(state.command == "sys"):
rsystem(state)
elif(state.command == "!sys"):
lsystem(state)
elif(state.command[0:5] == "mget "):
mget(state)
elif(state.command[0:5] == "mput "):
mput(state)
elif(state.command == "glob"):
toggle_glob(state)
elif(state.command[0:7] == "rename "):
rename(state)
elif(state.command == ""):
continue
else:
print("Incorrect command!")
state.control.close()