-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp-server.py
262 lines (212 loc) · 7.76 KB
/
ftp-server.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
import socket
import threading
import sys
import os
import pdb
command_port = 7711
data_port = command_port - 1
ftp_client_default_port = 7712 # we can't use default ports on the same computer, in a real ftp client the default port for the client is the command_port
data_thread_status = ""
class ftp_data_thread(threading.Thread):
def __init__(self, socket, cmd, data, filename=""):
self.data_socket = socket
self.cmd = cmd
self.data = data
self.filename = filename
threading.Thread.__init__(self)
def run(self):
if self.cmd == "list":
self.list()
elif self.cmd == "retr":
self.retr()
elif self.cmd == "stor":
self.stor(self.filename)
else:
print('unhandled data_thread command')
def retr(self):
global data_thread_status
try:
self.data_socket.connect(("127.0.0.1", 6548))
self.data_socket.sendall(bytearray(self.data))
self.data_socket.close()
data_thread_status = "226 Closing data connection"
except:
data_thread_status = "425 Can't open data connection"
def stor(self, filename):
global data_thread_status
try:
self.data_socket.connect(("127.0.0.1", 6548))
except:
data_thread_status = "425 Can't open data connection"
try:
try:
data = self.data_socket.recv(1024)
if data:
file = open(filename, "wb")
print (filename)
while True:
if not data:
break
print (data)
file.write(data)
data = self.data_socket.recv(1024)
file.close()
data_thread_status = "226 Closing data connection"
except:
data_thread_status = "problem receiving data"
except:
data_thread_status = "Can't open file status"
self.data_socket.close()
def list(self):
global data_thread_status
try:
self.data_socket.connect(("127.0.0.1", 6548))
self.data_socket.sendall(bytearray(self.data, "utf-8"))
self.data_socket.close()
data_thread_status = "226 Closing data connection"
except:
data_thread_status = "425 Can't open data connection"
class ftp_command_thread(threading.Thread):
def __init__(self, socket):
self.socket = socket
self.current_dir = os.path.abspath("./ftp-files/")
self.data_socket = None
threading.Thread.__init__(self)
self.finished_running = False
def run(self):
self.send_ctrl_response("220 awaiting input")
while True:
# wait for commands
cmd = str(self.socket.recv(1024), "utf-8")
if cmd:
if not cmd.endswith("\r\n"):
self.send_ctrl_response("500 Syntax error, command unrecognized.")
continue
else:
cmd = cmd.rstrip("\r\n")
split = cmd.lower().split(" ")
try:
# Dynamically call the command passing along the entire command array
getattr(self, split[0])(split)
except:
self.send_ctrl_response("500 Syntax error, command unrecognized.")
if self.finished_running:
return
def send_ctrl_response(self, message, encoding="utf-8"):
print ("Sending CTRL response: " + message)
self.socket.sendall(bytearray(message + "\r\n", encoding))
def quit(self, commands):
self.send_ctrl_response("221 closing control connection")
self.socket.close()
# Set finished_running so that the main loop will
# finish running
self.finished_running = True
def send_parameter_error_response(self):
self.send_ctrl_response("501 Syntax error in parameters or arguments.")
def list(self, commands):
if len(commands) > 2:
self.send_parameter_error_response()
return
# If there is a path given, use this, otherwise default
# to the current directory
dir = commands[1] if len(commands) == 2 else self.current_dir
data_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
files_in_dir = os.listdir(self.current_dir)
files_in_dir.sort()
file_string = ""
for file in files_in_dir:
file_string = file_string + file + "\n"
data_thread = ftp_data_thread(socket=data_socket, cmd="list", data=file_string)
self.send_ctrl_response('150 About to open data connection.')
data_thread.start()
data_thread.join()
# if data_thread_status == "":
# self.send_ctrl_response("226 Closing data connection, requested file action successful")
# else:
self.send_ctrl_response(data_thread_status)
except:
self.send_ctrl_response('451 Requested action aborted: local error in processing.')
def retr(self, commands):
if len(commands) is not 2:
self.send_parameter_error_response()
print("RETR error: No filename.")
return
filename = commands[1]
print ("server received retr cmd filename: " + filename)
filename = os.path.join(self.current_dir, filename)
print ("abs path: " + filename)
if not os.path.exists(filename):
self.send_ctrl_response("550 File Unavailable")
return
print ("file exists")
if os.access(filename, os.R_OK):
data_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
file_data = ""
try:
file = open(filename, 'rb')
file_data = file.read()
data_thread = ftp_data_thread(socket=data_socket, cmd="retr", data=file_data)
self.send_ctrl_response('150 About to open data connection.')
print('start retr thread')
data_thread.start()
data_thread.join()
if data_thread_status == "":
self.send_ctrl_response("226 Closing data connection, requested file action successful")
else:
self.send_ctrl_response(data_thread_status)
except:
self.send_ctrl_response("450 File Unavailable")
else:
self.send_ctrl_response("550 File Unavailable")
return
def stor(self, commands):
if len(commands) is not 2:
self.send_parameter_error_response()
print("STOR error: No filename.")
return
filename = commands[1]
filename = os.path.join(self.current_dir, filename)
#check for OS permissions or if the file does not exist we'll create it
if os.access(filename, os.R_OK) or not os.path.isfile(filename):
data_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
data_thread = ftp_data_thread(socket=data_socket, cmd="stor", data="", filename=filename)
self.send_ctrl_response('150 About to open data connection.')
data_thread.start()
data_thread.join()
if data_thread_status == "":
self.send_ctrl_response("226 Closing data connection, requested file action successful")
else:
self.send_ctrl_response(data_thread_status)
except:
self.send_ctrl_response("450 File Unavailable")
else:
self.send_ctrl_response("550 File Unavailable")
return
class ftp_server:
# basic class/socket setup
def __init__(self):
# self.port = 7711
self.server_socket = socket.socket()
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind(("localhost", command_port))
# listen for 1 client
self.server_socket.listen(1)
while True:
try:
print("waiting for connection on " + self.server_socket.getsockname()[0] + ":" + str(
self.server_socket.getsockname()[1]))
conn, addr = self.server_socket.accept()
print("accepted command connection: " + addr[0] + ":" + str(addr[1]))
fct = ftp_command_thread(conn)
fct.start()
except:
print("Unexpected error: ", sys.exc_info()[0])
print("Unexpected error: ", sys.exc_info()[1])
print("Unexpected error: ", sys.exc_info()[2])
self.server_socket.close()
exit()
if __name__ == '__main__':
# kick it off
server = ftp_server()