def put(self, data): """上传文件""" data = data.split(" ", 1) file_path = data[1].strip() file_name = os.path.basename(file_path) if os.path.isfile(file_path): header_file = { "name": file_name, "size": os.path.getsize(file_path), "md5": Public.get_md5(file_path) } header_file_json = json.dumps(header_file) header_file_len = struct.pack("i", len(header_file_json)) self.client.send(header_file_len) self.client.send(header_file_json.encode(settings.code)) if self.client.recv(settings.buffer_size).decode( settings.code) == "True": send_size = 0 with open(file_path, "rb") as f: for line in f: self.client.send(line) send_size += len(line) Public.progress_bar(send_size, header_file["size"]) if self.client.recv(settings.buffer_size).decode( settings.code) == "True": print("INFO:上传成功") self.log.info("%s上传%s文件成功" % (self.username, file_path)) else: print("ERROR:上传失败") self.log.error("%s上传%s文件失败" % (self.username, file_path)) else: print("Error:磁盘空间不足") else: print("Error:文件不存在")
def get(self, data): """下载文件""" check_file = self.client.recv(settings.buffer_size).decode( settings.code) if check_file == "True": header_len_bytes = self.client.recv(4) header_len = struct.unpack("i", header_len_bytes)[0] header = self.client.recv(header_len).decode(settings.code) header = json.loads(header) file_path = os.path.join(settings.download, header["name"]) recv_size = 0 with open(file_path, "wb") as f: while recv_size < header["size"]: line = self.client.recv(settings.buffer_size) f.write(line) recv_size += len(line) Public.progress_bar(recv_size, header["size"]) if Public.get_md5(file_path) == header["md5"]: print("INFO:下载成功") self.log.info("%s下载%s文件成功" % (self.username, file_path)) else: self.log.error("%s下载%s文件失败" % (self.username, file_path)) print("ERROR:下载失败") else: print("Error:文件不存在")
def put(self, data): """ client上传文件操作 :param data: :return: """ header_len_bytes = self.conn.recv(4) header_len = struct.unpack("i", header_len_bytes)[0] header = self.conn.recv(header_len).decode(settings.code) header = json.loads(header) file_path = os.path.join(self.home_path, header["name"]) user_home_dir = os.path.join( os.path.join(settings.home_dir, self.username)) if self.check_home_size(user_home_dir, header["size"]): self.conn.send("True".encode(settings.code)) recv_size = 0 with open(file_path, "wb") as f: while recv_size < header["size"]: line = self.conn.recv(settings.buffer_size) f.write(line) recv_size += len(line) else: self.conn.send("False".encode(settings.code)) return if Public.get_md5(file_path) == header["md5"]: self.conn.send("True".encode(settings.code)) self.log.info("%s上传了%s文件" % (self.username, header["name"])) else: self.conn.send("False".encode(settings.code))
def __init__(self, username, password): self.username = username self.__password = password self.__home_size = settings.home_size self.user_info = settings.user_info self.user_size = settings.user_size self.user_dic = MyJson.load(self.user_info) self.user_size_dic = MyJson.load(self.user_size) self.log = Public.log()
def __init__(self, host, port): self.host = host self.port = port self.server = socket.socket() self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server.bind((self.host, self.port)) self.server.listen() self.home_dir = settings.home_dir self.status = False self.log = Public.log() self.user_size = settings.user_size self.user_size_dic = MyJson.load(self.user_size)
def run(self): """启动client,用户输入指令""" Public.helper() try: if self.login(): print("INFO:登录成功,请输入指令") self.log.info("%s登录成功" % self.username) while True: data = input(">>>:").strip() if not data: continue cmd = data.split()[0] if hasattr(self, cmd): self.client.send(data.encode(settings.code)) getattr(self, cmd)(data) else: print("Error:命令不存在") except ConnectionResetError as e: self.log.error(e) print("Error:Ftp server异常,请重新连接") except Exception as e: self.log.error(e) print(e)
def check_home_size(self, home_path, recv_size): """ 检测用户剩余空间 :param home_path: 用户家目录 :param recv_size: 上传文件大小 :return: """ Public.sum_size = 0 home_size = self.user_size_dic[self.username] * 1024 * 1024 user_size = Public.dir_size(home_path) surplus_size = home_size - user_size if surplus_size < recv_size: self.log.error("%s磁盘空间不足" % self.username) return False else: return True
def du(self, data): """获取用户存储使用情况""" Public.sum_size = 0 if len(data) != 1: self.conn.send("False".encode(settings.code)) return else: self.conn.send("True".encode(settings.code)) total = self.user_size_dic[self.username] * 1024 * 1024 use = Public.dir_size(os.path.abspath(os.path.join(os.path.join(settings.home_dir, self.username)))) residual = total - use status = {"User": self.username, "Total_size": "%s M" % (total/1024/1024), "Use_size": "%s M" % (use/1024/1024), "Residual_size": "%s M" % (residual/1024/1024)} print(status) status_json_bytes = json.dumps(status).encode(settings.code) self.conn.send(status_json_bytes)
def get(self, data): """ client下载文件操作 :param data: :return: """ file_path = os.path.join(self.home_path, data[1].strip()) if os.path.isfile(file_path): self.conn.send("True".encode(settings.code)) header_file = {"name": data[1].strip(), "size": os.path.getsize(file_path), "md5": Public.get_md5(file_path)} header_file_json = json.dumps(header_file) header_file_len = struct.pack("i", len(header_file_json)) self.conn.send(header_file_len) self.conn.send(header_file_json.encode(settings.code)) with open(file_path, "rb") as f: for line in f: self.conn.send(line) self.log.info("%s下载了%s文件" % (self.username, data[1].strip())) else: self.conn.send("False".encode(settings.code))
def handle(self): """启动server等待用户输入指令""" self.home_dir = settings.home_dir self.status = False self.log = Public.log() self.user_size = settings.user_size self.user_size_dic = MyJson.load(self.user_size) print("<---Server start---->") while True: self.conn = self.request self.address = self.client_address print(self.address) if not self.status: try: while True: if self.login(): self.conn.send("True".encode(settings.code)) self.log.info("%s登录成功" % self.username) self.home_path = os.path.abspath(os.path.join(self.home_dir, self.username)) break else: self.conn.send("False".encode(settings.code)) self.log.info("%s登录失败" % self.username) continue while self.status: data = self.conn.recv(settings.buffer_size).decode(settings.code).split(" ", 1) cmd = data[0] if hasattr(self, cmd): getattr(self, cmd)(data) else: print("命令不存在") break except Exception as e: print(e) self.log.error(e) finally: self.status = False self.conn.close()
def __init__(self, host, port): self.host = host self.port = port self.client = socket.socket() self.client.connect((host, port)) self.log = Public.log()