首页 > 其他分享 >文件上传ssh 并显示进度条

文件上传ssh 并显示进度条

时间:2023-10-11 11:37:08浏览次数:38  
标签:remote 进度条 local ssh file path 上传 size

import os
import paramiko
import time


def download_file_with_progress(hostname, port, username, password, remote_path, local_path):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, port=port, username=username, password=password)
sftp = ssh.open_sftp()
# get remote file size
remote_file_size = sftp.stat(remote_path).st_size
print("remote_file_size:{}".format(remote_file_size))
# print progress
with open(local_path, 'wb') as f:
def callback(transferred, remote_file_size):
percent = float(transferred) * 100 / remote_file_size
print("Download %.2f%% of the file." % percent)

# transfer 32768 bytes as SSH slice, get remote file to local
sftp.getfo(remote_path, f, callback=callback)
sftp.close()
ssh.close()


def upload_file_with_progress(hostname, username, password, remote_path, local_path):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=username, password=password)
sftp = ssh.open_sftp()
# get local file size
local_file_size = os.stat(local_path).st_size
print("local_file_size:{}".format(local_file_size))
# print progress
lst = []
try:
with open(local_path, 'rb') as f:
def callback(transferred, local_file_size):
percent = int(float(transferred) * 100 / local_file_size)
if percent not in lst:
lst.append(percent)
if percent == 5:
sftp.remove(remote_path)
raise
print("Upload %.2f%% of the file." % percent)
# transfer 32768 bytes as SSH slice, put local file to remote
sftp.putfo(f, remote_path, local_file_size, callback=callback)
except Exception as e:

print("error:",e)
finally:
sftp.close()
ssh.close()



if __name__ == "__main__":
# hostname = "*****"
hostname = "*********"
port = 22
# username = "********"
username = "*******"
# password = "*****"
password = "*********"
remote_path = "/F:/liu/test_ssh/mibcsv.csv"
local_path = "./mibcsv.csv"


# # download file and calculate cost
# print("Download Start, 32678 bytes as default SSH slice: ")
# download_start = time.time()
# download_file_with_progress(hostname, port, username, password, remote_path, local_path)
# download_end = time.time()
# download_cost = download_end - download_start
# print("Download successfully! Cost {}s".format(download_cost))
#
# cutline = '\033[32;1m%s\033[0m' % '=' * 100
# print(cutline)



# upload file and calculate cost
print("Upload Start, 32678 bytes as default SSH slice: ")
# remote_path_upload = "/F:/liu/test_ssh/anconda"
remote_path_upload = "/F:/liu/test_ssh/anconda"
local_path_upload = "./anconda"
upload_start = time.time()
upload_file_with_progress(hostname, username, password, remote_path_upload, local_path_upload)
upload_end = time.time()
upload_cost = upload_end - upload_start
print("Upload successfully! Cost {}s".format(upload_cost))

标签:remote,进度条,local,ssh,file,path,上传,size
From: https://www.cnblogs.com/daien522556/p/17756646.html

相关文章

  • 中断某个用户的所有SSH连接
    使用pkill命令:如果你知道SSH会话的用户名,你可以使用pkill命令来中断该用户的所有SSH会话。例如,要中断用户名为username的所有SSH会话,可以运行以下命令:pkill-uusernamessh......
  • Bug实录——配置了Github SSHKey之后还需要输入密码
    问题:换了一台新机器,pull了源码进行了更新,然后配置了github的sshkey,但在push时还是提示要输入密码,但我已经在github上关闭了密码提交权限(出于安全和便捷考虑)。然后就提交失败了。分析:可能是我是先pull的代码,然后才去设置的sskkey,导致本地仓库并没有同步到这个sshKey配置信息解......
  • macOS Ventura配置ssh/key无效的问题记录
    内容转载自https://cloud.tencent.com/developer/article/2149714此处仅做个人记录问题描述工作电脑是macOSVentura,需要连接gitlab仓库,下载安装git并初始化配置后,按照操作生成SSHkey后,连接远程仓库仍然报错提示Permissiondenied(publickey)。定位问题经过查证,macOS......
  • java如何做大体积的文件上传和下载
    在Java中,实现大体积文件的上传和下载涉及到处理文件的分片、并发上传、断点续传等问题。本文将详细介绍如何通过Java实现大体积文件的上传和下载。1.文件上传文件上传是将本地文件上传到服务器的过程。对于大体积文件的上传,我们可以将文件分成多个小片段进行并发上传。1.1文件......
  • 如何优雅的上传博客
    平常使用markdown(Typora)写文档很方便,在各式各样的平台展示都不成问题。但是图片上传博客平台却很不方便,上传git平台另说。这里介绍一个本人修改的博客图片上传工具,xle97/dotnet-cnblogs......
  • 前端如何实现大文件上传
    在开发过程中,经常会遇到一些较大文件上传,如果只使用一次请求去上传文件,一旦这次请求中出现什么问题,那么无论这次上传了多少文件,都会失去效果,用户则需要重新上传所有资源。所以就想到一种方式,将一个大文件分成多个小文件,这样通过多个请求实现大文件上传。接下来我们就来看看具体......
  • git上传至公共或私有github
    1.下载gitbash参考链接:https://git-scm.com/download2.创建git的秘钥gitconfig--globaluser.name"githubname"gitconfig--globaluser.email"githubemail"ssh-keygen-trsa-C"githubemail"其中:githubname是你的名称,githubemail是你的邮箱3.添加de......
  • SSH key免密登录
    ls-a可以查看隐藏文件SSHkey免密登录流程*只有第三步中使用了靶机,其余的都是虚拟机中的操作。1、客户机生成密钥对ssh-keygen密码都选空。 2、将公钥发送到免密登录的主机(靶机),中间会要求输入一次密码作为验证[email protected] 3、在靶机上可以看到发送......
  • Selenium借助AutoIt完成文件的上传与下载
    文件上传1,编辑首先提前下载好AutoIT,先了解https://blog.csdn.net/weixin_39218743/article/details/87808776手上没有带上传文件的网址,先用百度的上传照片吧!打开AutoIT工具组件中的脚本编辑器sciTEScriptEditorWinWaitActive("打开")Send('D:\img\11.jpg')Sleep(2000)Send("{......
  • 前端大文件上传方法
    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现。在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表格数据、上传影音文件等。如果文件体积比较大,或者网络条件不好时,上传的时间会......