Python 通过SFTP 实现文件的批量上传
程序员文章站
2023-11-24 19:07:22
文章目录Python - Python 通过SFTP 进行文件的批量上传1、引用之前的 auto_template,并增加上传目录的方法2、主要代码3、运行代码4、运行结果Python - Python 通过SFTP 进行文件的批量上传1、引用之前的 auto_template,并增加上传目录的方法import paramikoimport timefrom dir import dir_utils# 读取命令执行后的终端返回def read_unicode(unicode):...
Python - Python 通过SFTP 进行文件的批量上传
1、引用之前的 auto_template,并增加上传目录的方法
import paramiko
import time
from dir import dir_utils
# 读取命令执行后的终端返回
def read_unicode(unicode):
return str(unicode, 'utf-8').split('\n')
# 执行命令
def command(ssh_config, cmd, result_print=None):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
password=ssh_config.password)
print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
if result_print:
lines = read_unicode(result)
for line in lines:
print(line)
ssh.close()
# 上传目录和文件
def upload_dirs(ssh_config, local, server):
client, transport = sftp(ssh_config)
rel_dirs, rel_filenames = dir_utils.foreach(local)
# 创建根目录
path_cmd = dir_utils.linux_path_cmd(server)
command(ssh_config, path_cmd)
# 创建子目录
for path in rel_dirs:
server_dir = server + path.replace(local, '')
path_cmd = dir_utils.linux_path_cmd(server_dir)
command(ssh_config, path_cmd)
# 上传文件
for file in rel_filenames:
server_path = server + file.replace(local, '')
print('FILE UPLOAD ↑↑↑ LOCAL:', file, ' -> SERVER: ', server_path)
client.put(file, server_path)
transport.close()
# 上传指定文件
def upload_new(ssh_config, local, server):
client, transport = sftp(ssh_config)
client.put(local, server)
transport.close()
def sftp(ssh_config):
transport = paramiko.Transport(ssh_config.hostname, ssh_config.port)
transport.connect(username=ssh_config.username, password=ssh_config.password)
client = paramiko.SFTPClient.from_transport(transport)
return client, transport
def auto_deploy(deploy_config, ssh_config):
print('Step 1 : stop application')
command(ssh_config, deploy_config.stop_cmd)
print('wait for application stop')
time.sleep(2)
print('Step 2 : delete old application file')
command(ssh_config, deploy_config.delete_cmd)
print('Step 3 : upload new application file to server')
print('wait for application upload')
upload_new(ssh_config, deploy_config.local, deploy_config.server)
print('upload completed')
print('Step 4 : run application')
time.sleep(2)
command(ssh_config, deploy_config.start_cmd)
print('deploy application completed')
2、主要代码
# 上传目录和文件
def upload_dirs(ssh_config, local, server):
client, transport = sftp(ssh_config)
rel_dirs, rel_filenames = dir_utils.foreach(local)
# 创建根目录
path_cmd = dir_utils.linux_path_cmd(server)
command(ssh_config, path_cmd)
# 创建子目录
for path in rel_dirs:
server_dir = server + path.replace(local, '')
path_cmd = dir_utils.linux_path_cmd(server_dir)
command(ssh_config, path_cmd)
# 上传文件
for file in rel_filenames:
server_path = server + file.replace(local, '')
print('FILE UPLOAD ↑↑↑ LOCAL:', file, ' -> SERVER: ', server_path)
client.put(file, server_path)
transport.close()
3、运行代码
from auto_deploy.nsbd import ssh_config
from auto_deploy import auto_template
if __name__ == '__main__':
server_path = '/tmp/lllllll/'
local_path = 'C:/Users/A-PC/Desktop/123/'
auto_template.command(ssh_config, 'rm -rf ' + server_path)
auto_template.upload_dirs(ssh_config, local_path, server_path)
4、运行结果
47.114.154.255@root : rm -rf /tmp/lllllll/
47.114.154.255@root : [ ! -d "/tmp/lllllll/" ] && mkdir /tmp/lllllll/
47.114.154.255@root : [ ! -d "/tmp/lllllll/4444777" ] && mkdir /tmp/lllllll/4444777
47.114.154.255@root : [ ! -d "/tmp/lllllll/5555" ] && mkdir /tmp/lllllll/5555
47.114.154.255@root : [ ! -d "/tmp/lllllll/66666" ] && mkdir /tmp/lllllll/66666
47.114.154.255@root : [ ! -d "/tmp/lllllll/4444777/212554" ] && mkdir /tmp/lllllll/4444777/212554
47.114.154.255@root : [ ! -d "/tmp/lllllll/4444777/2222" ] && mkdir /tmp/lllllll/4444777/2222
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/123.txt -> SERVER: /tmp/lllllll/123.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/1234567.txt -> SERVER: /tmp/lllllll/1234567.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/324324324.txt -> SERVER: /tmp/lllllll/4444777/324324324.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/gdfsgdgf.txt -> SERVER: /tmp/lllllll/4444777/gdfsgdgf.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/212554/1551.txt -> SERVER: /tmp/lllllll/4444777/212554/1551.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/2222/545.txt -> SERVER: /tmp/lllllll/4444777/2222/545.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/5555/4324.txt -> SERVER: /tmp/lllllll/5555/4324.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/66666/54545.txt -> SERVER: /tmp/lllllll/66666/54545.txt
本文地址:https://blog.csdn.net/qq_15071263/article/details/107085213