def putMySQL(configItem, file): try: conn = mysql.connector.connect(host=configItem.dir, user=configItem.username, password=configItem.password, database=configItem.target) cur = conn.cursor() with open(file, 'rb') as f: data = pickle.load(f) #拼凑导入sql table = os.path.basename(file).split('.')[0] sql = 'INSERT INTO ' + table + ' VALUES(' length = len(data[0]) for i in range(length): sql += '%s' if i < length - 1: sql += ',' sql += ')' #导入 for i in data: exec(cur, sql, i) conn.commit() conn.close() except Exception as e: LOGR.error('写入数据库出错:' + str(e)) return False return True
def putSMB(task, file): try: samba = SMBConnection(task.username, task.password, 'mybase', task.target) samba.connect(task.dir, int(task.port)) list = task.tables.split(' ') share = list[0] #TESt path = list[1] #/ s = file.split('/')[1] l = len(s) + 1 + 5 t = file[l:] #temp/share/ ddddd if path[-1] == '/': path += t #/ddd else: path = path + '/' + t # 取路径 dir = os.path.split(path)[0] # 去除首位空格 dir = dir.strip() # 去除尾部 \ 符号 dir = dir.rstrip("\\") if dir != '/': mkdir(samba, share, dir) with open(file, 'rb') as f: samba.storeFile(share, path, f) return True except Exception as e: LOGR.error('发送SMB文件错误:' + str(e))
def putFile(task, file): try: path = task.dir.strip("/") path.strip() isExists = os.path.exists(path) if not isExists: os.makedirs(path) shutil.copy(file, path) return True except Exception as e: LOGR.error('文件复制错误:' + str(e)) return False
def putFTP(task, file): try: conn = MyFTP(task.target, int(task.port), str(task.finger)) conn.Login(task.username, task.password) conn.ftp.cwd(task.dir.rstrip()) path = file[5:] print(path) conn.UpLoadFile(file, path) conn.quit() return True except Exception as e: LOGR.error('FTP上传错误:' + str(e)) return False
def worker(): try: r = redis.Redis() except Exception as e: LOGR('redis连接失败,进程退出!') print(str(e)) return -1 while True: file = r.rpop('file') if file is not None: run(file.decode()) time.sleep(1)
def run(file): list = file.split('.') id = int(list[-1]) #file_name = file.split('/')[-1] new_file = file[:-(len(list[-1]) + 1)] os.rename(file, new_file) pass #解析任务 db.connect() try: task = Task.get(Task.finger == id) except: LOGR.debug('找不到对应任务:' + str(id)) task = Task() task.finger = id task.name = '' task.dataType = '0' task.dir = '' task.username = '' task.password = '' task.target = '' task.port = '' task.tables = '' task.count = 0 task.save() LOGR.info('发现新任务:' + str(id)) exit(-1) #增加运行次数 task.count = task.count + 1 task.save() db.close() LOGR.info('开始任务:' + str(id) + ":" + task.name) result = putData(task, new_file) os.remove(new_file) if result: LOGR.info('任务执行完成:' + file) else: LOGR.info('任务执行失败:' + file)
def worker(): try: r = redis.Redis() except Exception as e: LOGR('redis连接失败,进程退出!') print(str(e)) return -1 while True: file = r.rpop('file') if file is not None: run(file.decode()) time.sleep(1) if __name__ == '__main__': p1 = multiprocessing.Process(target=worker) p1.daemon = True p1.start() LOGR.info('接收进程1启动成功') p2 = multiprocessing.Process(target=worker) p2.daemon = True p2.start() LOGR.info('接收进程2启动成功') p1.join() p2.join()