forked from Sephoro/FTP-Application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpClient.py
341 lines (254 loc) · 9.96 KB
/
ftpClient.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
# client.py
# Authors - Boikanyo Radiokana (1386807)
# Authors - Elias Sepuru (1427726)
import socket
import math
import sys
import time
import os
class FTPclient:
def __init__(self, clientName):
self.IPsocket = None
self.DTPsocket = None
self.errorResp = False
self.alive = False
self.loggedIn = False
self.user = None
self.remotedirList = []
self.collectMSG = []
self.statusMSG = ' '
self.clientName = clientName
def initConnection(self, serverIPname, serverIPport):
self.serverIPname = serverIPname
self.serverIPport = serverIPport
self.IPsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Try to connect to server
try:
self.IPsocket.connect((self.serverIPname, self.serverIPport))
print(self.IPsocket.recv(8192).decode())
except:
errMSG = 'Failed to connect ' + self.serverIPname
self.statusMSG = errMSG
print(errMSG)
self.errorResp = True
time.sleep(3)
return
self.alive = True
print('Connected to Server ;)')
def getStatus(self):
return self.statusMSG
def login(self, userName, password):
# enter username
cmd = 'USER ' + userName
self.send(cmd)
self.printServerReply(self.getServerReply())
if not self.errorResp:
# enter password
cmd = 'PASS ' + password
self.send(cmd)
self.printServerReply(self.getServerReply())
if not self.errorResp:
self.loggedIn = True
self.user = userName
msg =('Login Success\n')
print(msg)
self.statusMSG = msg
def send(self, cmd):
# Sending commands to server
self.IPsocket.send((cmd + '\r\n').encode())
# Dont print or log the password
if cmd[:4] != 'PASS':
print('Client: ', cmd)
self.collectMSG.append('Client: ' + cmd)
def getServerReply(self):
resp = self.IPsocket.recv(8192).decode()
self.collectMSG.append('Server: ' + resp)
# Notify if this an error
if resp[0] != '5' and resp[0] != '4':
self.errorResp = False
else:
self.errorResp = True
return resp
def printServerReply(self, resp):
print('Server :', resp)
def setMode(self, mode):
# Set mode of data transfer
if mode.upper() == 'I' or mode.upper() == 'A':
self.mode = mode
cmd = 'TYPE ' + mode
self.send(cmd)
self.printServerReply(self.getServerReply())
else:
msg = ('Client : Error unknown mode')
self.statusMSG = msg
print(msg)
def getComm(self):
return self.collectMSG
def clearComm(self):
self.collectMSG.clear()
def startPassiveDTPconnection(self):
# Ask for a passive connection
cmd = 'PASV'
self.send(cmd)
resp = self.getServerReply()
self.printServerReply(resp)
if not self.errorResp:
firstIndex = resp.find('(')
endIndex = resp.find(')')
# Obtain the server DTP address and Port
addr = resp[firstIndex+1:endIndex].split(',')
self.serverDTPname = '.'.join(addr[:-2])
self.serverDTPport = (int(addr[4]) << 8) + int(addr[5])
print(self.serverDTPname, self.serverDTPport)
try:
# Connect to the server DTP
self.DTPsocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
self.DTPsocket.connect(
(self.serverDTPname, self.serverDTPport))
self.statusMSG = 'Passive Connection Success, Ready to receive'
print('Passive Connection Success, Ready to receive\n')
self.dataConnectionAlive = True
except:
print('Failed to connect to ', self.serverDTPname)
self.statusMSG = 'Failed to connect to '+ self.serverDTPname
self.dataConnectionAlive = False
time.sleep(3)
return
def startActiveConnection(self):
# Request for an active connection
self.clientSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.clientSocket.bind((self.clientName,0))
self.clientSocket.listen(1)
ip, port = self.clientSocket.getsockname()
# Ready the ip with respect to the RFC959 standard
ip = ip.split('.')
ip = ','.join(ip)
# Ready the Port with respect to the TFC959 standard
p1 = math.floor(port/256)
p2 = port%256
print('Requested...\n IP: ' + ip + '\nPort: ' + str(port))
self.statusMSG = 'Requested... IP: ' + ip + 'Port: ' + str(port)
cmd = 'PORT ' + ip + ',' + str(p1) + ',' + str(p2)
self.send(cmd)
self.printServerReply(self.getServerReply())
# Start Connection
self.DTPsocket, addr = self.clientSocket.accept()
print('Connected to :' , addr)
self.statusMSG = 'Connected to :' + str(addr)
self.dataConnectionAlive = True
def getList(self):
self.remotedirList = []
# Cant't get list if disconnected
if self.dataConnectionAlive and self.alive:
cmd = 'LIST'
self.send(cmd)
self.printServerReply(self.getServerReply())
print('\nReceiving Data...\n')
self.statusMSG = 'Receiving Data...'
while True:
# Get the directory list
data = self.DTPsocket.recv(1024)
print(data.decode())
self.remotedirList.append(data.decode())
if not data:
break
print('Directory Listing Done!\n')
self.statusMSG ='Directory Listing Done!'
self.DTPsocket.close()
self.printServerReply(self.getServerReply())
def downloadFile(self,fileName):
# Send download command
cmd = 'RETR ' + fileName
self.send(cmd)
self.printServerReply(self.getServerReply())
# Dont continue if there is an error
if not self.errorResp:
# Create Downloads folder if not exist
downloadFolder = 'Downloads'
if not os.path.exists(downloadFolder):
os.makedirs(downloadFolder)
# Mode of data transfer
if self.mode == 'I':
outfile = open(downloadFolder + '/' + fileName, 'wb')
else:
outfile = open(downloadFolder + '/' + fileName, 'w')
# Get them packets :D
print('Receiving data...')
self.statusMSG = 'Recieving data...'
while True:
data = self.DTPsocket.recv(8192)
if not data:
break
outfile.write(data)
outfile.close()
# Done
print('Transfer Succesfull')
self.statusMSG = 'Transfer Successfull'
self.DTPsocket.close()
self.printServerReply(self.getServerReply())
def uploadFile(self,filePath):
#Check if file path is valid
if os.path.exists(filePath):
# Get the file name
if '/' in filePath:
f_index = filePath.rindex('/')
fileName = filePath[f_index+1:]
else:
fileName = filePath
# Send Command
cmd = 'STOR ' + fileName
self.send(cmd)
self.printServerReply(self.getServerReply())
# Continue if there are no errors reported
if not self.errorResp:
print('Uploading ' + fileName + ' to server...')
self.statusMSG = 'Uploading ' + fileName + ' to server...'
if self.mode == 'I':
uFile = open(filePath, 'rb')
else:
uFile = open(filePath, 'r')
# Send packets of the file
data = uFile.read(8192)
while data:
if self.mode == 'I':
self.DTPsocket.send(data)
else:
self.DTPsocket.send(data.encode())
data = uFile.read(8192)
uFile.close()
print('Upload success')
self.statusMSG = ' Upload Success'
self.DTPsocket.close()
self.printServerReply(self.getServerReply())
else:
print('Error: invalid path!')
self.statusMSG = 'Error: invalid path!'
self.DTPsocket.close()
def returnDirList(self):
return self.remotedirList
def changeWD(self,dir_):
# Change working directory
cmd = 'CWD ' + dir_
self.send(cmd)
self.printServerReply(self.getServerReply())
def makeDir(self,folderName):
# Create a new directory on server
cmd = 'MKD ' + folderName
self.send(cmd)
self.printServerReply(self.getServerReply())
def remDir(self,folderName):
# Delete directory on server
cmd = 'RMD ' + folderName
self.send(cmd)
self.printServerReply(self.getServerReply())
def logout(self):
#Close connection
cmd = 'QUIT'
self.send(cmd)
self.printServerReply(self.getServerReply())
self.statusMSG = 'Logged out, Connection Closed'
def checkConnection(self):
cmd = 'NOOP'
self.send(cmd)
self.printServerReply(self.getServerReply())