def wrap_get_weights(self, mock_conn): logger = Logger('weights-vapor-connector.py') logger.customer = 'EGI' logger.job = self.jobs[0] self.orig_get_weights.im_func.func_globals['globopts'] = self.globopts self.orig_get_weights.im_func.func_globals['input'].connection.func = mock_conn self.orig_get_weights.im_func.func_globals['logger'] = logger return self.orig_get_weights()
def wrap_get_downtimes(self, start, end, mock_conn): logger = Logger('downtimes-gocdb-connector.py') logger.customer = 'EGI' logger.job = self.jobs[0] self.orig_get_downtimes.im_func.func_globals['globopts'] = self.globopts self.orig_get_downtimes.im_func.func_globals['input'].connection.func = mock_conn self.orig_get_downtimes.im_func.func_globals['logger'] = logger return self.orig_get_downtimes(start, end)
def pwd(self, command): if len(command.split()) == 1: self.request.sendall(b'203') Logger.info('[%s] 执行 %s' % (self.client_address, command)) response = self.request.recv(1024) self.request.sendall(self.user_current_path.encode()) else: self.request.sendall('403'.encode())
def wrap_loadProfilesFromServer(self, server, vo, namespace, profiles): logger = Logger('poem-connector.py') logger.customer = 'EGI' logger.job = self.jobs[1] self.orig_loadProfilesFromServer.im_func.func_globals['globopts'] = self.globopts self.orig_loadProfilesFromServer.im_func.func_globals['input'].connection.func = self.mock_conn self.orig_loadProfilesFromServer.im_func.func_globals['logger'] = logger return self.orig_loadProfilesFromServer(server, vo, namespace, profiles)
def pwd(self, command): if len(command.split()) == 1: self.request.sendall(b'202') Logger.info('[%s] 执行成功.' % command) response = self.request.recv(1024) self.request.sendall(self.user_current_path.encode()) else: Logger.warning('[%s] 命令格式错误.' % command) self.request.sendall(b'402')
def dir(self, command): if len(command.split()) == 1: self.request.sendall(b'203') Logger.info('[%s] 执行 %s' % (self.client_address, command)) response = self.request.recv(1024) send_data = os.popen('dir %s' % self.user_current_path) self.request.sendall(send_data.read().encode()) else: self.request.sendall('403'.encode())
def mkdir(self, command): if len(command.split()) > 1: dir_name = command.split()[1] dir_path = os.path.join(self.user_current_path, dir_name) if not os.path.isdir(dir_path): Logger.info('[%s] 执行成功.' % command) self.request.sendall(b'202') response = self.request.recv(1024) os.makedirs(dir_path) else: Logger.warning('[%s] 命令格式错误.' % command) self.request.sendall(b'402')
def login(self): user_list = Auth.file_oper(settings.USER_LIST_FILE, 'r').split('\n')[:-1] if self.user in user_list: user_db_file = os.path.join(settings.DB_PATH, self.user) + '.db' user_pickle = Auth.file_oper(user_db_file, 'rb') user_dict = pickle.loads(user_pickle) if self.user == user_dict['user'] and self.pwd == user_dict['pwd']: Logger.info('[%s]登录成功.' % self.user) return user_dict else: Logger.error('[%s]用户名不存在.' % self.user)
def dir(self, command): if len(command.split()) == 1: Logger.info('[%s] 执行成功.' % command) self.request.sendall(b'202') response = self.request.recv(1024) cmd_res = subprocess.Popen('dir %s' % self.user_current_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout = cmd_res.stdout.read() stderr = cmd_res.stderr.read() result = stdout if stdout else stderr self.request.sendall(result) else: Logger.warning('[%s] 命令格式错误.' % command) self.request.sendall(b'402')
def login(self): if os.path.isfile(settings.USER_NAME_FILE): user_list = Auth.file_oper(settings.USER_NAME_FILE, 'r').split('\n')[:-1] print('user_list:', user_list) if self.username in user_list: user_db_file = os.path.join(settings.DATABASE_DIR, self.username) + '.db' user_info = Auth.file_oper(user_db_file, 'rb', user_db_file) user_dict = pickle.loads(user_info) print('user_dict:', user_dict) if self.username == user_dict[ 'username'] and self.password == user_dict['password']: return '200', user_dict else: Logger.warning('[%s]用户名未注册.' % self.username) return '402'
def register(self): sql = "SELECT user from userinfos where user = %s " params = [self.user] helper = MysqlHelper() result = helper.fetchone(sql, params) if result == None: user_home_path = os.path.join(settings.HOME_PATH, self.user) if not os.path.isdir(user_home_path): os.makedirs(user_home_path) sql = 'insert into userinfos(user, pwd, email, home_path, limit_size) values(%s,%s,%s,%s,%s)' params = [self.user, self.pwd, self.email, user_home_path, settings.LIMIT_SIZE] helper = MysqlHelper() row = helper.insert(sql, params) # print(row) Logger.info('[%s]注册成功。' % self.user) return '201' else: Logger.warning('[%s]注册用户名已存在。' % self.user) return '401'
def cd(self, command): if len(command.split()) > 1: dir_name = command.split()[1] dir_path = os.path.join(self.user_current_path, dir_name) if dir_name == '..' and len(self.user_current_path) > len( self.user_home_path): self.request.sendall(b'202') response = self.request.recv(1024) self.user_current_path = os.path.dirname( self.user_current_path) elif os.path.isdir(dir_path): self.request.sendall(b'202') response = self.request.recv(1024) if dir_name != '.' and dir_name != '..': self.user_current_path = dir_path else: self.request.sendall(b'403') else: Logger.warning('[%s] 命令格式错误.' % command) self.request.sendall(b'402')
def register(self): if os.path.isfile(settings.USER_NAME_FILE): user_list = Auth.file_oper(settings.USER_NAME_FILE, 'r').split('\n')[:-1] print('user_list:', user_list) if self.username not in user_list: Auth.file_oper(settings.USER_NAME_FILE, 'a', self.username + '\n') user_home_path = os.path.join(settings.HOME_DIR, self.username) if not os.path.isdir(user_home_path): os.makedirs(user_home_path) user_db_file = os.path.join(settings.DATABASE_DIR, self.username) + '.db' user_dict = { 'username': self.username, 'password': self.password, 'home_path': user_home_path, 'limit_size': settings.LIMIT_FILE } user_info = pickle.dumps(user_dict) Auth.file_oper(user_db_file, 'ab', user_info) Logger.info('%s 注册成功.' % self.username) return '201' else: Logger.warning('[401][%s]用户名已存在,请重新注册.' % self.username) return '401' else: Logger.error('用户列表文件不存在,请先创建.') print('\033[31;1m用户列表文件不存在,请先创建.\033[0m') return '501'
def register(self): user_list = Auth.file_oper(settings.USER_LIST_FILE, 'r').split('\n')[:-1] if self.user not in user_list: Auth.file_oper(settings.USER_LIST_FILE, 'a', self.user + '\n') user_home_path = os.path.join(settings.HOME_PATH, self.user) if not os.path.isdir(user_home_path): os.makedirs(user_home_path) user_dict = { 'user': self.user, 'pwd': self.pwd, 'home_path': user_home_path, 'limit_size': settings.LIMIT_SIZE } user_pickle = pickle.dumps(user_dict) user_db_file = os.path.join(settings.DB_PATH, self.user) + '.db' Auth.file_oper(user_db_file, 'ab', user_pickle) Logger.info('[%s]注册成功.' % self.user) return '201' else: Logger.warning('[%s]注册用户名已存在.' % self.user) return '401'
def cd(self, command): if len(command.split()) > 1: dir_name = command.split()[1] dir_path = os.path.join(self.user_current_path, dir_name) print('dir_path', dir_path) if dir_name == '..' and len(self.user_current_path) > len( self.user_home_path): self.request.sendall(b'202') response = self.request.recv(1024) self.user_current_path = os.path.dirname( self.user_current_path) elif os.path.isdir(dir_path): self.request.sendall(b'202') response = self.request.recv(1024) if dir_name != '.' and dir_name != '..': self.user_current_path = os.path.join( self.user_current_path, dir_name) print(self.user_current_path) else: Logger.error('%s 目录不存在.' % dir_path) self.request.sendall(b'403') else: self.request.sendall(b'404')
def handle(self): while True: try: user_info = self.request.recv(1024).decode() if not user_info: continue Logger.info('[%s:%s] 连接成功.' % self.client_address) auth_type = user_info.split(':')[-1] auth = Auth(user_info) if auth_type == 'register': auth_code = auth.register() self.request.sendall(auth_code.encode()) # if auth_code != '201': # continue elif auth_type == 'login': auth_code, user_dict = auth.login() self.user_home_path = user_dict['home_path'] self.user_current_path = user_dict['home_path'] self.limit_size = user_dict['limit_size'] self.request.sendall(auth_code.encode()) print('auth_code:', auth_code) if auth_code != '200': continue while True: command = self.request.recv(1024).decode() print(command) if not command: continue command_str = command.split()[0] if hasattr(self, command_str): func = getattr(self, command_str) func(command) # cmd = self.request.recv(1024).decode() # cmd = input('>>>') except ConnectionResetError as e: print('Error:', e) Logger.info('[%s:%s] 断开连接.' % self.client_address) break
def login(self): sql = "SELECT user,pwd from userinfos where user = %s " params = [self.user] helper = MysqlHelper() result = helper.fetchone(sql, params) if result[0] is not None: if self.pwd == result[1]: sql = 'select user, email, home_path, limit_size from userinfos where user = %s' helper = MysqlHelper() user_dict = helper.fetchone(sql, params) Logger.info('[%s]登录成功.' % self.user) return user_dict else: Logger.error('[%s]用户名或密码错误.' % self.user) else: Logger.warning('[%s]登录用户不存在.' % self.user)
def put(self, command): filename = command.split()[1] file_path = os.path.join(self.user_current_path, filename) response = self.request.sendall(b'000') file_size = self.request.recv(1024).decode() file_size = int(file_size) used_size = self.getdirsize(self.user_home_path) if self.user_limit_size > file_size + used_size: self.request.sendall(b'202') Logger.info('[%s] 执行成功.' % command) recv_size = 0 Logger.info('[%s] 文件开始上传.' % file_path) with open(file_path, 'wb') as f: while recv_size != file_size: data = self.request.recv(1024) recv_size += len(data) f.write(data) Logger.info('[%s] 文件上传完成.' % file_path) else: self.request.sendall(b'403')