python上传本地文件_python3写的简单本地文件上传服务器实例

#!/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 = ssh.exec_command('find ' + path)

result = stdout.read().decode('utf-8')

if len(result) == 0 :

print('目录 %s 不存在,创建目录' % path)

ssh.exec_command('mkdir ' + path)

print('%s 创建成功' % path)

return 1

else:

print('目录 %s 已存在' % path)

return 0

# 检查文件是否存在,不存在直接上传,存在检查大小是否一样,不一样则上传

def check_file(local_path, ssh_path):

# 检查文件是否存在,不存在直接上传

stdin, stdout, stderr = ssh.exec_command('find ' + ssh_path)

result = stdout.read().decode('utf-8')

if len(result) == 0 :

sftp.put(local_path,ssh_path)

print('%s 上传成功' % (ssh_path))

return 1

else:

# 存在则比较文件大小

# 本地文件大小

lf_size = os.path.getsize(local_path)

# 目标文件大小

stdin, stdout, stderr = ssh.exec_command('du -b ' + ssh_path)

result = stdout.read().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('上传开始')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python3文件从一个服务器传输到另一个服务器,可以使用paramiko模块实现。这个模块允许你使用SSH协议连接到远程服务器并执行文件传输操作。 以下是实现文件传输的简单示例代码: ``` import paramiko # 设置源和目标服务器的主机名、用户名和密码 src_hostname = 'source_server' src_username = 'source_username' src_password = 'source_password' dst_hostname = 'destination_server' dst_username = 'destination_username' dst_password = 'destination_password' # 创建SSH客户端连接到源服务器 src_ssh = paramiko.SSHClient() src_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) src_ssh.connect(src_hostname, username=src_username, password=src_password) # 创建SSH客户端连接到目标服务器 dst_ssh = paramiko.SSHClient() dst_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) dst_ssh.connect(dst_hostname, username=dst_username, password=dst_password) # 创建SFTP客户端连接到源服务器 src_sftp = src_ssh.open_sftp() # 在源服务器上打开要传输的文件 local_path = '/path/to/local/file' remote_path = '/path/to/remote/file' src_file = src_sftp.open(local_path, 'rb') # 在目标服务器上打开要传输的文件 dst_sftp = dst_ssh.open_sftp() dst_file = dst_sftp.open(remote_path, 'wb') # 从源服务器读取数据并入目标服务器 dst_file.write(src_file.read()) # 关闭文件和SFTP连接 src_file.close() dst_file.close() src_sftp.close() dst_sftp.close() # 关闭SSH连接 src_ssh.close() dst_ssh.close() ``` 这个示例代码使用paramiko模块连接到源服务器和目标服务器,并使用SFTP协议进行文件传输。在示例代码中,你需要将`src_hostname`、`src_username`、`src_password`、`dst_hostname`、`dst_username`和`dst_password`设置为源服务器和目标服务器的实际信息,将`local_path`和`remote_path`设置为要传输的文件的本地路径和远程路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值