python3写的简单本地⽂件上传服务器实例
python是个很好玩的东西?好吧我随⼝说的,反正因为各种原因(其实到底是啥我也不知道),简单的学习了下python,然后写了⼀个上传⽂件上服务器的⼩玩具练⼿。
⼤概功能是这样:
1、获取本地⽂件列表(包括⽂件夹)
2、检查服务器上是否存在,不存在直接上传,存在的话,⽂件夹⽆视,⽂件⽐较⼤⼩,⼤⼩不⼀致则覆盖,最后检查服务器上是否存在本地没有的⽂件,存在则删除
3、之后增加了忽略列表,忽略⽂件类型
4、然后增加了重启tomcat,但是这个功能未进⾏测试
⼤概就是这个样⼦,哦了,丢代码丢代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import os.path
import paramiko
import datetime
import re
# 配置属性
config = {
#本地项⽬路径
'local_path' : '',
# 服务器项⽬路径
'ssh_path' : '',
# 项⽬名
'project_name' : '',
# 忽视列表
'ignore_list' : [],
# ssh地址、端⼝、⽤户名、密码
'hostname' : '',
'port' : 22,
'username' : '',
'password' : '',
# 是否强制更新
'mandatory_update' : False,
# 更新完成后是否重启tomcat
'restart_tomcat' : False,
# tomcat bin地址
'tomcat_path' : '',
# 被忽略的⽂件类型
'ignore_file_type_list' : []
}
# 检查⽂件夹是否存在,不存在则创建
def check_folder(path):
stdin, stdout, stderr = _command('find ' + path)
result = ad().decode('utf-8')
if len(result) == 0 :
print('⽬录 %s 不存在,创建⽬录' % path)
<_command('mkdir ' + path)
print('%s 创建成功' % path)
return 1
else:
print('⽬录 %s 已存在' % path)
return 0
# 检查⽂件是否存在,不存在直接上传,存在检查⼤⼩是否⼀样,不⼀样则上传
def check_file(local_path, ssh_path):
# 检查⽂件是否存在,不存在直接上传
stdin, stdout, stderr = _command('find ' + ssh_path)
result = ad().decode('utf-8')
if len(result) == 0 :
sftp.put(local_path,ssh_path)
print('%s 上传成功' % (ssh_path))
return 1
else:
# 存在则⽐较⽂件⼤⼩
# 本地⽂件⼤⼩
lf_size = size(local_path)
# ⽬标⽂件⼤⼩
stdin, stdout, stderr = _command('du -b ' + ssh_path)
result = ad().decode('utf-8')
tf_size = int(result.split('\t')[0])
print('本地⽂件⼤⼩为:%s,远程⽂件⼤⼩为:%s' % (lf_size, tf_size))
if lf_size == tf_size:
print('%s ⼤⼩与本地⽂件相同,不更新' % (ssh_path))
return 0
else:
sftp.put(local_path,ssh_path)
print('%s 更新成功' % (ssh_path))
return 1
# 上传流程开始
print('上传开始')
begin = w()
# ⽂件夹列表
folder_list = []
# ⽂件列表
file_list = []
# ssh上⽂件列表
ssh_file_list = []
for parent,dirnames,filenames in os.walk(config['local_path']+config['project_name']):
#初始化⽂件夹列表
for dirname in dirnames:
p = os.path.join(parent,dirname)
folder_list.append(p[p.find(config['project_name']):])
#初始化⽂件列表
for filename in filenames:
if config['ignore_list'].count(filename) == 0:
p = os.path.join(parent,filename)
file_list.append(p[p.find(config['project_name']):])
print('共有⽂件夹%s个,⽂件%s个' % (len(folder_list),len(file_list)))
# ssh控制台
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
transport = paramiko.Transport((config['hostname'],config['port']))
sftp = paramiko.SFTPClient.from_transport(transport)
# 检查根⽬录是否存在
root_path = config['ssh_path'] + config['project_name']
stdin, stdout, stderr = _command('find ' + root_path)
result = ad().decode('utf-8')
if len(result) == 0 :
print('⽬录 %s 不存在,创建⽬录' % root_path)
<_command('mkdir ' + root_path)
print('%s 创建成功' % root_path)
else:
print('⽬录 %s 已存在,获取所有⽂件' % root_path)
ssh_file_list = re.split('\n',result)
# 检查⽂件夹
create_folder_num = 0
for item in folder_list:
target_folder_path = config['ssh_path'] + item
create_folder_num = create_folder_num + check_folder(target_folder_path)
# 检查⽂件
update_file_num = 0
for item in file_list:
if config['ignore_file_type_list'].count(os.path.splitext(item)[1]) == 0:
local_file_path = config['local_path'] + item
target_file_path = config['ssh_path'] + item
if config['mandatory_update']:
sftp.put(local_file_path,target_file_path)
print('%s 强制更新成功' % (target_file_path))
update_file_num = update_file_num + 1
100个python练手项目else:
update_file_num = update_file_num + check_file(local_file_path, target_file_path)
else:
print('%s 在被忽略⽂件类型中,所以被忽略' % item)
# 检查ssh是否有需要删除的⽂件
delete_file_num = 0
for item in ssh_file_list:
temp = item[item.find(config['project_name']):]
if unt(temp) == 0 and unt(temp) == 0 and temp != config['project_name'] and temp != '':
print('%s 在本地不存在,删除' % item)
<_command('rm -rf ' + item)
delete_file_num = delete_file_num + 1
end = w()
print('本次上传结束:创建⽂件夹%s个,更新⽂件%s个,删除⽂件%s个,耗时:%s' % (create_folder_num, update_file_num, delete_file_num, end-begin)) if config['restart_tomcat']:
print('关闭tomcat')
<_command('sh ' + config['tomcat_path'] + 'shutdown.sh')
print('启动tomcat')
<_command('sh ' + config['tomcat_path'] + 'startup.sh')
# 关闭连接
sftp.close()
ssh.close()
最后加了个强制更新,即不管三七⼆⼗⼀只要不在忽略列表中直接上传覆盖,毕竟通过⽐较⼤⼩来更新⽂件有概率出问题,⽐如我把1改成2那么通过⽂件⼤⼩是⽆法对⽐出来的,如果之后有时间的话会琢磨下拉取git更新记录或者别的⽅案。
以上这篇python3写的简单本地⽂件上传服务器实例就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家
多多⽀持。