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
|
''' @File : apps.py @Time : 2021/12/14 16:34:56 @Author : Li Ruilong @Version : 1.0 @Contact : 1224965096@qq.com @Desc : 一个从mysql数据库获取文件路径,通过scp远程下载文件到本地的脚本
pip install pymysql pip install paramiko '''
import pymysql import os import time import paramiko
host = '127.0.0.1' port = 3306 user = 'user' password = '***********' db = 'dbname'
ssh_hostname = "127.0.0.1" ssh_username = "user" ssh_password = '***********'
def initDB(): '''连接数据库的操作 Args: host(str) port(int) user(str) password(str) db(str) Returns: 连接状态:1成功,0失败 ''' try: global connection connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) global cursor cursor = connection.cursor() print("数据库连接成功============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================", '\n') return 1 except: print("数据库连接异常============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================", '\n') return 0
def scp(local_filename, remote_path): '''创建`scp`连接, Args: local_filename(str): 本地要存放的文件位置 remote_path(int) 远程的文件位置 Returns: void ''' ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy()) ssh.connect(ssh_hostname, port=22, username=ssh_username, password=ssh_password)
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport()) sftp = ssh.open_sftp() sftp.get(remote_path, local_filename)
def execute(): sql = '''SELECT a.number,a.path,a.date FROM tablename a WHERE (number LIKE "%{}" OR numbers LIKE "%{}" ) AND year(a.date) ="{}" AND month(a.date) = "{}" '''
print("查询sql:",sql)
year = input("请输入年份:") month = input("请输入月份:") number = input("请输入电话号码:") print("\n") sql = sql.format(number, number, year, month) print("数据查询中请稍等.....") resout = cursor.execute(sql) if(resout == 0): print("没有需要的数据!!!", '\n') time.sleep(5) else: date = cursor.fetchall() for i in date: pathName = i["path"] print("获取到的文件位置:", pathName, '\n') fileName = str(pathName).split("/")[7] print("文件名称:", fileName, '\n') currentPath = os.getcwd() loadPathName = currentPath+"\\"+fileName try: scp(loadPathName, pathName) print("下载成功============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================", '\n') print("下载后的文件路径:", loadPathName, '\n') except: print("下载异常!!!!", '\n') time.sleep(5)
if __name__ == '__main__': print('数据库连接','\n') if (initDB() == 1): while True: boo = input("是否下载录音文件:?y/n\n") if boo == 'y': execute() else: break else: print("数据库链接异常")
|