def receive_file(sk, filename): sk.send(package("size")) data_size = int(sk.recv(service.configure.msg_buffer).decode()) sk.send(package("data")) fp = open(filename, "wb") while data_size: if data_size > service.configure.file_buffer: data = sk.recv(service.configure.file_buffer) else: data = sk.recv(data_size) fp.write(data) data_size -= len(data) fp.close()
def compute_svd(self, src_name, dst_name, r=0.9): self.sk.send(package("svd")) response(self.sk, "name", src_name) response(self.sk, "rstname", dst_name) response(self.sk, "r", str(r)) rst = self.sk.recv(service.configure.msg_buffer).decode() return rst
def compute_pca(self, src_name, dst_name, n=0): self.sk.send(package("pca")) response(self.sk, "name", src_name) response(self.sk, "rstname", dst_name) response(self.sk, "n", str(n)) rst = self.sk.recv(service.configure.msg_buffer).decode() return rst
def remove(self, name): self.sk.send(package("remove")) response(self.sk, "name", name) rst = self.sk.recv(service.configure.msg_buffer).decode() if rst == service.object_pool.success_msg: return name + " has been removed" else: return rst
def compute_feature(self, src_name, dst_name, mtd, option, main, unpack=False): self.sk.send(package("feature")) response(self.sk, "name", src_name) response(self.sk, "rstname", dst_name) response(self.sk, "mtd", str(mtd)) response(self.sk, "option", str(option)) response(self.sk, "main", str(main)) response(self.sk, "unpack", str(unpack)) rst = self.sk.recv(service.configure.msg_buffer).decode() return rst
def exec(self, file): self.sk.send(package("execute")) index = file.rfind(os.path.sep) filename = file[index + 1:] response(self.sk, "filename", filename) send_file(self.sk, file) wait_cmd(self.sk, "output") receive_file(self.sk, "output") with open("output", "r") as fp: data = fp.read() os.remove("output") return data
def load(self, name): self.sk.send(package("load")) response(self.sk, "name", name) filename = self.sk.recv(service.configure.msg_buffer).decode() if filename == service.configure.no_such_object: return None state = self.sk.recv(service.configure.msg_buffer).decode() if state != service.object_pool.success_msg: return None receive_file(self.sk, filename) if filename.split(".")[1] == "svm": tmp_svm = calculate.classify.SVM.SVM() tmp_svm.load(filename) var = tmp_svm elif filename.split(".")[1] == "knn": tmp_knn = calculate.classify.KNN.KNN() tmp_knn.load(filename) var = tmp_knn else: var = DAL.file.load(filename) os.remove(filename) return var
def save(self, var, name): self.sk.send(package("save")) data_type = str(type(var))[8:-2] if data_type == "calculate.classify.SVM.SVM": data_type = "svm" elif data_type == "calculate.classify.KNN.KNN": data_type = "knn" filename = name + "." + data_type if data_type == "svm": var.save(filename) elif data_type == "knn": var.save(filename) else: DAL.file.save(var, filename) response(self.sk, "name", name) response(self.sk, "type", data_type) state = self.sk.recv(service.configure.msg_buffer).decode() if state != service.object_pool.success_msg: return state send_file(self.sk, filename) os.remove(filename) return state
def run(self): try: self.connect.send(package("start")) except ConnectionError: time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [%s:%s] Abnormal connection." % (time_str, self.address[0], self.address[1])) self.connect.close() return if service.configure.save_path[-1] != os.path.sep: service.configure.save_path += os.path.sep if service.configure.exec_path[-1] != os.path.sep: service.configure.exec_path += os.path.sep while True: try: cmd = self.connect.recv(service.configure.cmd_buffer).decode() if len(cmd) != 0: time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [%s:%s] " % (time_str, self.address[0], self.address[1]) + cmd) if cmd == "close": self.connect.close() break if cmd == "save": data_name = request(self.connect, "name") data_type = request(self.connect, "type") filename = service.configure.save_path + data_name + "." + data_type rst = self.object_pool.add_object(data_name, data_type, 0) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue rst = self.object_pool.lock_object(data_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue self.connect.send(package(service.object_pool.success_msg)) receive_file(self.connect, filename) self.object_pool.unlock_object(data_name) self.object_pool.update_object(data_name) elif cmd == "load": data_name = request(self.connect, "name") filename = self.object_pool.filename_object(data_name) if filename is None: self.connect.send( package(service.configure.no_such_object)) continue else: self.connect.send(package(filename)) rst = self.object_pool.lock_object(data_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue self.connect.send(package(service.object_pool.success_msg)) send_file(self.connect, service.configure.save_path + filename) self.object_pool.unlock_object(data_name) elif cmd == "remove": data_name = request(self.connect, "name") rst = self.object_pool.del_object(data_name) self.connect.send(package(rst)) elif cmd == "list": obj_dict = self.object_pool.copy_entry_object() self.connect.send(package(str(obj_dict))) elif cmd == "rename": data_name = request(self.connect, "name") rename_name = request(self.connect, "rename") rst = self.object_pool.rename_object( data_name, rename_name) self.connect.send(package(rst)) elif cmd == "clone": data_name = request(self.connect, "name") clone_name = request(self.connect, "clonename") rst = self.object_pool.clone_object(data_name, clone_name) self.connect.send(package(rst)) elif cmd == "execute": filename = request(self.connect, "filename") filename = service.configure.exec_path + filename count = 0 real_filename = filename while os.path.exists(real_filename): index = filename.rfind(".") real_filename = filename[:index] + str( count) + filename[index:] count += 1 filename = real_filename receive_file(self.connect, filename) outfile = filename + ".out" with open(filename, "r", encoding="utf-8") as fp: data = fp.read() data.replace("\r\n", "\n") head = "import sys\nfp = open(r\"%s\", \"w\")\nsys.stdout = fp\nsys.stderr = fp\n" % outfile data = head + data with open(filename, "w", encoding="utf-8") as fp: fp.write(data) os.system(service.configure.python + " " + filename) self.connect.send(package("output")) send_file(self.connect, outfile) elif cmd == "file": files = os.listdir(service.configure.exec_path) file_info = [] for file in files: if file[-3:] != ".py": continue file_info.append( (file, os.path.getctime(service.configure.exec_path + file))) self.connect.send(package(str(file_info))) elif cmd == "delete": file = request(self.connect, "file") if file == "*": files = os.listdir(service.configure.exec_path) for file in files: os.remove(service.configure.exec_path + file) else: if not os.path.exists(service.configure.exec_path + file): self.connect.send( package("No such python file: " + file)) os.remove(service.configure.exec_path + file) if os.path.exists(service.configure.exec_path + file + ".out"): os.remove(service.configure.exec_path + file + ".out") self.connect.send(package(service.object_pool.success_msg)) elif cmd == "feature": data_name = request(self.connect, "name") rst_name = request(self.connect, "rstname") mtd = request(self.connect, "mtd") option = eval(request(self.connect, "option")) main = eval(request(self.connect, "main")) unpack = eval(request(self.connect, "unpack")) if not self.object_pool.avilable_object(data_name): self.connect.send( package(service.object_pool.unavilable_msg + data_name)) continue if self.object_pool.have_object(rst_name): self.connect.send( package(service.object_pool.exist_msg + rst_name)) continue self.object_pool.add_object(rst_name, "dict", 0) rst = self.object_pool.lock_object(rst_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue data_dict = self.object_pool.get_object(data_name) try: for key, value in data_dict.items(): data_dict[key] = calculate.feature.core.method( mtd, value, option, main, unpack) self.object_pool.save_object(rst_name, data_dict) self.object_pool.update_object(rst_name) self.object_pool.unlock_object(rst_name) except Exception as e: self.object_pool.unlock_object(rst_name) self.connect.send(package(str(e))) continue self.connect.send(package(service.object_pool.success_msg)) elif cmd == "pca": data_name = request(self.connect, "name") rst_name = request(self.connect, "rstname") n = int(request(self.connect, "n")) if not self.object_pool.avilable_object(data_name): self.connect.send( package(service.object_pool.noexist_msg + data_name)) continue if self.object_pool.have_object(rst_name): self.connect.send( package(service.object_pool.exist_msg + rst_name)) continue self.object_pool.add_object(rst_name, "dict", 0) rst = self.object_pool.lock_object(rst_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue data_dict = self.object_pool.get_object(data_name) try: data_dict = calculate.helper.PCA.pca(data_dict, n) self.object_pool.save_object(rst_name, data_dict) self.object_pool.update_object(rst_name) self.object_pool.unlock_object(rst_name) except Exception as e: self.object_pool.unlock_object(rst_name) self.connect.send(package(str(e))) continue self.connect.send(package(service.object_pool.success_msg)) elif cmd == "svd": data_name = request(self.connect, "name") rst_name = request(self.connect, "rstname") r = float(request(self.connect, "r")) if not self.object_pool.avilable_object(data_name): self.connect.send( package(service.object_pool.noexist_msg + data_name)) continue if self.object_pool.have_object(rst_name): self.connect.send( package(service.object_pool.exist_msg + rst_name)) continue self.object_pool.add_object(rst_name, "dict", 0) rst = self.object_pool.lock_object(rst_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue data_dict = self.object_pool.get_object(data_name) try: data_dict = calculate.helper.SVD.svd(data_dict, r) self.object_pool.save_object(rst_name, data_dict) self.object_pool.update_object(rst_name) self.object_pool.unlock_object(rst_name) except Exception as e: self.object_pool.unlock_object(rst_name) self.connect.send(package(str(e))) continue self.connect.send(package(service.object_pool.success_msg)) except Exception as e: time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [%s:%s] %s" % (time_str, self.address[0], self.address[1], str(e))) self.connect.close() break time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [Close connection] %s:%s" % (time_str, self.address[0], self.address[1]))
def delete(self, file): self.sk.send(package("delete")) response(self.sk, "file", file) rst = self.sk.recv(service.configure.msg_buffer).decode() return rst
def file(self): self.sk.send(package("file")) rst = self.sk.recv(service.configure.msg_buffer).decode() return eval(rst)
def close(self): self.sk.send(package("close")) self.sk.close()
def clone(self, src_name, dst_name): self.sk.send(package("clone")) response(self.sk, "name", src_name) response(self.sk, "clonename", dst_name) rst = self.sk.recv(service.configure.msg_buffer).decode() return rst
def run(self): try: self.connect.send(package("start")) except ConnectionError: time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [%s:%s] Abnormal connection." % (time_str, self.address[0], self.address[1])) self.connect.close() return if service.configure.save_path[-1] != os.path.sep: service.configure.save_path += os.path.sep if service.configure.exec_path[-1] != os.path.sep: service.configure.exec_path += os.path.sep while True: try: cmd = self.connect.recv(service.configure.cmd_buffer).decode() if len(cmd) != 0: time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [%s:%s] " % (time_str, self.address[0], self.address[1]) + cmd) if cmd == "close": self.connect.close() break if cmd == "save": data_name = request(self.connect, "name") data_type = request(self.connect, "type") filename = service.configure.save_path + data_name + "." + data_type rst = self.object_pool.add_object(data_name, data_type, 0) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue rst = self.object_pool.lock_object(data_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue self.connect.send(package(service.object_pool.success_msg)) receive_file(self.connect, filename) self.object_pool.unlock_object(data_name) self.object_pool.update_object(data_name) elif cmd == "load": data_name = request(self.connect, "name") filename = self.object_pool.filename_object(data_name) if filename is None: self.connect.send(package(service.configure.no_such_object)) continue else: self.connect.send(package(filename)) rst = self.object_pool.lock_object(data_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue self.connect.send(package(service.object_pool.success_msg)) send_file(self.connect, service.configure.save_path + filename) self.object_pool.unlock_object(data_name) elif cmd == "remove": data_name = request(self.connect, "name") rst = self.object_pool.del_object(data_name) self.connect.send(package(rst)) elif cmd == "list": obj_dict = self.object_pool.copy_entry_object() self.connect.send(package(str(obj_dict))) elif cmd == "rename": data_name = request(self.connect, "name") rename_name = request(self.connect, "rename") rst = self.object_pool.rename_object(data_name, rename_name) self.connect.send(package(rst)) elif cmd == "clone": data_name = request(self.connect, "name") clone_name = request(self.connect, "clonename") rst = self.object_pool.clone_object(data_name, clone_name) self.connect.send(package(rst)) elif cmd == "execute": filename = request(self.connect, "filename") filename = service.configure.exec_path + filename count = 0 real_filename = filename while os.path.exists(real_filename): index = filename.rfind(".") real_filename = filename[: index] + str(count) + filename[index:] count += 1 filename = real_filename receive_file(self.connect, filename) outfile = filename + ".out" with open(filename, "r", encoding="utf-8") as fp: data = fp.read() data.replace("\r\n", "\n") head = "import sys\nfp = open(r\"%s\", \"w\")\nsys.stdout = fp\nsys.stderr = fp\n" % outfile data = head + data with open(filename, "w", encoding="utf-8") as fp: fp.write(data) os.system(service.configure.python + " " + filename) self.connect.send(package("output")) send_file(self.connect, outfile) elif cmd == "file": files = os.listdir(service.configure.exec_path) file_info = [] for file in files: if file[-3:] != ".py": continue file_info.append((file, os.path.getctime(service.configure.exec_path + file))) self.connect.send(package(str(file_info))) elif cmd == "delete": file = request(self.connect, "file") if file == "*": files = os.listdir(service.configure.exec_path) for file in files: os.remove(service.configure.exec_path + file) else: if not os.path.exists(service.configure.exec_path + file): self.connect.send(package("No such python file: " + file)) os.remove(service.configure.exec_path + file) if os.path.exists(service.configure.exec_path + file + ".out"): os.remove(service.configure.exec_path + file + ".out") self.connect.send(package(service.object_pool.success_msg)) elif cmd == "feature": data_name = request(self.connect, "name") rst_name = request(self.connect, "rstname") mtd = request(self.connect, "mtd") option = eval(request(self.connect, "option")) main = eval(request(self.connect, "main")) unpack = eval(request(self.connect, "unpack")) if not self.object_pool.avilable_object(data_name): self.connect.send(package(service.object_pool.unavilable_msg + data_name)) continue if self.object_pool.have_object(rst_name): self.connect.send(package(service.object_pool.exist_msg + rst_name)) continue self.object_pool.add_object(rst_name, "dict", 0) rst = self.object_pool.lock_object(rst_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue data_dict = self.object_pool.get_object(data_name) try: for key, value in data_dict.items(): data_dict[key] = calculate.feature.core.method(mtd, value, option, main, unpack) self.object_pool.save_object(rst_name, data_dict) self.object_pool.update_object(rst_name) self.object_pool.unlock_object(rst_name) except Exception as e: self.object_pool.unlock_object(rst_name) self.connect.send(package(str(e))) continue self.connect.send(package(service.object_pool.success_msg)) elif cmd == "pca": data_name = request(self.connect, "name") rst_name = request(self.connect, "rstname") n = int(request(self.connect, "n")) if not self.object_pool.avilable_object(data_name): self.connect.send(package(service.object_pool.noexist_msg + data_name)) continue if self.object_pool.have_object(rst_name): self.connect.send(package(service.object_pool.exist_msg + rst_name)) continue self.object_pool.add_object(rst_name, "dict", 0) rst = self.object_pool.lock_object(rst_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue data_dict = self.object_pool.get_object(data_name) try: data_dict = calculate.helper.PCA.pca(data_dict, n) self.object_pool.save_object(rst_name, data_dict) self.object_pool.update_object(rst_name) self.object_pool.unlock_object(rst_name) except Exception as e: self.object_pool.unlock_object(rst_name) self.connect.send(package(str(e))) continue self.connect.send(package(service.object_pool.success_msg)) elif cmd == "svd": data_name = request(self.connect, "name") rst_name = request(self.connect, "rstname") r = float(request(self.connect, "r")) if not self.object_pool.avilable_object(data_name): self.connect.send(package(service.object_pool.noexist_msg + data_name)) continue if self.object_pool.have_object(rst_name): self.connect.send(package(service.object_pool.exist_msg + rst_name)) continue self.object_pool.add_object(rst_name, "dict", 0) rst = self.object_pool.lock_object(rst_name) if rst != service.object_pool.success_msg: self.connect.send(package(rst)) continue data_dict = self.object_pool.get_object(data_name) try: data_dict = calculate.helper.SVD.svd(data_dict, r) self.object_pool.save_object(rst_name, data_dict) self.object_pool.update_object(rst_name) self.object_pool.unlock_object(rst_name) except Exception as e: self.object_pool.unlock_object(rst_name) self.connect.send(package(str(e))) continue self.connect.send(package(service.object_pool.success_msg)) except Exception as e: time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [%s:%s] %s" % (time_str, self.address[0], self.address[1], str(e))) self.connect.close() break time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print("%s [Close connection] %s:%s" % (time_str, self.address[0], self.address[1]))