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
| import os import socketserver import threading import time
def add_opr_file(client_addr, item): """ 向FTPDataHandler类的操作列表中添加新操作 命令通道和数据通道,实际上是通过FTPDataHandler类中的client_oper字典联系在一起的 :param client_addr: 客户端的ip地址 :param item: 新操作 """ if client_addr in FTPDataHandler.client_oper: FTPDataHandler.client_oper[client_addr].append(item) else: FTPDataHandler.client_oper[client_addr] = [item, ]
class FTPHandler(socketserver.StreamRequestHandler): def __init__(self, request=None, client_addr=None, server=None): """ FTP服务器的处理类 :param request: 请求对象,即连接的客户端socket :param client_addr: 客户端地址 :param server: 与自己绑定的服务器对象(即后面的MyThreadFTPServer对象) """ self.cmd_keys = ("QUIT", "USER", "NOOP", "TYPE", "PASV", "PORT", "RETR", "STOR") self.coms = {} self.__init_coms() self.server = server self.cmd_port = 21 self.data_port = 20 self.pasv_data_ip = None self.pasv_data_port = None self.args = None self.loged = False self.pasv_mode = None super().__init__(request, client_addr, server)
def __init_coms(self): """ 初始化字典coms,键为命令名字,值为具体的方法 """ for key in self.cmd_keys: self.coms[key] = getattr(self, "exe_" + key.lower())
def handle(self): """ 重写父类的处理函数 """ while True: cmds = self.rfile.readline() if not cmds: continue cmds = cmds.decode("utf-8") cmd = self.__parse_cmd(cmds) if cmd in self.cmd_keys: self.coms[cmd]() else: self.__send(500, "Invalid command.") if cmd == "QUIT": break
def __parse_cmd(self, cmds): """ 从字符串中提取命令动词和参数 :param cmds:包含命令、参数的字符串 :return:命令动词,大写 """ if ' ' in cmds: cmd, args = cmds.split(' ') self.args = args.strip('\n').strip() else: cmd = cmds.strip('\n').strip() return cmd.upper()
def __send(self, code, info): """ 向客户端返回命令执行状态 :param code:状态码 :param info:状态信息 """ infos = "%d %s\n" % (code, info) self.request.sendall(infos.encode("utf-8"))
def __make_pasv_info(self): """ 返回进入主动模式的信息 :return: 返回给客户端的信息,包括主动模式数据通道的IP地址和端口号,按照FTP协议的格式要求发送 """ ip_info = self.pasv_data_ip.split('.') ip_info = ','.join(ip_info) porta_info = str(self.pasv_data_port // 256) portb_info = str(self.pasv_data_port % 256) return ','.join((ip_info, porta_info, portb_info))
def __enter_pasv(self): """ 进入被动模式,开启数据服务器 """ if not self.server.data_server: self.pasv_data_ip, self.pasv_data_port = self.server.create_data_server()
def exe_quit(self): """ QUIT命令的执行动作 """ self.__send(221, "bye.")
def exe_user(self): """ USER命令的执行动作 """ user = self.args if user in ("", "anonymous"): self.loged = True self.__send(230, "identified!") else: self.__send(530, "Only use anonymous.")
def exe_noop(self): """ NOOP命令的执行动作 """ self.__send(200, "ok.")
def exe_type(self): """ TYPE命令的执行动作 """ self.__send(200, "ok.")
def exe_pasv(self): """ PASV命令的执行动作 """ if not self.loged: self.__send(332, "Please login.") return if self.pasv_mode: info = "entering passive mode (%s)" % self.__make_pasv_info() self.__send(227, info) return try: self.__enter_pasv() info = "entering passive mode (%s)" % self.__make_pasv_info() self.pasv_mode = True self.__send(227, info) except Exception as e: print(e) self.pasv_mode = False self.__send(500, "Fail to enter passvie mode.")
def exe_port(self): """ PORT命令的执行动作 """ self.__send(500, "Do not support port mode.")
def exe_retr(self): """ RETR命令的执行动作 """ if not os.path.exists(os.path.join("/root", "server", self.args)): self.__send(550, "File {file_path} not exist!".format(file_path=self.args)) return client_addr = self.request.getpeername()[0] add_opr_file(client_addr, ("RETR", self.args)) self.__send(150, "ok.")
def exe_stor(self): """ STOR命令的执行动作 """ client_addr = self.request.getpeername()[0] add_opr_file(client_addr, ("STOR", self.args)) self.__send(150, "ok.")
class FTPDataHandler(socketserver.StreamRequestHandler): client_oper = {}
def handle(self): """ 数据服务器针对每个连接的客户端的操作 """ peerip = self.request.getpeername()[0] opr = self.__get_opr_args(peerip) if opr: if opr[0] == "RETR": self.retr_file(opr[1]) elif opr[0] == "STOR": self.stor_file(opr[1]) self.request.close()
def __get_opr_args(self, peerip): """ 根据IP地址查询客户端需要服务的内容 :param peerip: 远端的IP地址 :return: opr具体的操作 """ if peerip in self.client_oper: opr = self.client_oper[peerip].pop(0) if not self.client_oper[peerip]: self.client_oper.pop(peerip) return opr
def retr_file(self, filepath): """ 客户端下载文件(对应服务器端的发送文件) :param filepath: 文件路径 """ filepath = os.path.join("/root", "server", filepath) print(filepath) f = open(filepath, "rb") while True: data = f.read(1024) if data: self.request.sendall(data) else: break f.close()
def stor_file(self, filepath): """ 客户端上传文件(对应服务器端的接收并保存文件) :param filepath:文件路径 """ filepath = os.path.join("/root", "server", filepath) print(filepath) f = open(filepath, "wb") while True: data = self.request.recv(1024) if data: f.write(data) else: break f.close()
class MyThreadFTPServer(socketserver.ThreadingTCPServer): def __init__(self, addr, handler): """ 自定义多线程FTP服务器 :param addr: 服务器地址,元祖(IP地址,端口号),21命令端口,20数据端口 :param handler:处理客户端连接的对象 """ self.data_server = None super().__init__(addr, handler)
def shutdown(self): """ 重写父类的shutdown函数 """ if self.data_server: threading.Thread(target=self.data_server.shutdown).start() super().shutdown()
def create_data_server(self): """ 创建数据通道专用服务器 :return:pasv_data_ip数据服务器的IP地址;pasv_data_port数据服务器的端口号 """ self.data_server = socketserver.ThreadingTCPServer(("127.0.0.1", 0), FTPDataHandler) pasv_data_ip, pasv_data_port = self.data_server.server_address threading.Thread(target=self.data_server.serve_forever).start() return pasv_data_ip, pasv_data_port
if __name__ == "__main__": ftp_server = MyThreadFTPServer(("127.0.0.1", 21), FTPHandler) threading.Thread(target=ftp_server.serve_forever).start() print("FTP Server Start...")
|