今天接到一个小需求,就是想在windows环境下,上传压缩文件到linux指定的目录位置并且解压出来,然后我想了一下,这个可以用python试试写下。

环境:
1.linux操作系统一台
2.windows环境安装Python3.x
3.Pycharm IDE环境
大家如果对paramiko模块不熟悉的话,可以去了解哈。

上传文件用到SFTPCLient,SFTPCLient作为一个sftp的客户端对象,根据ssh传输协议的sftp会话,实现远程文件操作,如上传、下载、权限、状态。

代码如下

 1 #!/usr/bin/env python
2 import paramiko
3 import sys
4
5 src_file = sys.argv[1]  #windows下的源文件
6 dsc_path = sys.argv[2]  #linux上的目录路径
7 key_path = '私钥路径'
8 password = '密码'
9 ssh = paramiko.SSHClient() # 创建SSH对象
10 private_key = paramiko.RSAKey.from_private_key_file(key_path) # 指定私钥
11 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 允许连接不在know_hosts文件中的主机
12 transport = paramiko.Transport(('ip', 端口号))
13 transport.connect(username='root', pkey=private_key) # 连接服务器
14
15 def upload():
16 sftp = paramiko.SFTPClient.from_transport(transport)
17 try:
18 sftp.put(src_file, dsc_path + '/' + src_file)
19 print('上传成功')
20 except Exception as e:
21 print(e)
22 ssh.close()
23
24
25 def unzip():
26 ssh._transport = transport
27 stdin, stdout, stderr = ssh.exec_command('cd ' + dsc_path + '&&' + 'unzip -o ' + src_file)
28 result = stdout.read().decode()
29 if result.strip() != '':
30 print('解压成功')
31 else:
32 print('Error message ' + stderr.read().decode())
33 ssh.close()
34
35
36 if __name__ == '__main__':
37 upload()
38 unzip()

可以分别通过Pycharm或者cmd来测试脚本运行,下面是用pycharm测试,把apps.zip文件上传到Linux服务器的/tmp目录下,并且解压

1 python upload_file.py apps.zip /tmp
2 上传成功
3 解压成功

然后登陆到Linux服务器的/tmp目录查看是否有压缩文件并且解压

总结:这只是一个简单上传解压功能,请多多指教。

python实现上传文件到linux指定目录的更多相关文章

  1. 使用putty上传文件到linux系统

    使用window的cmd命令 上传文件到linux  使用putty下的 pscp.exe pscp -r -l root -pw 1234567890 e:/htk 192.168.0.204:/r ...

  2. Windows上传文件到linux 使用winscp

    Windows上传文件到linux 使用winscp, winscp下载目录 https://sourceforge.net/projects/winscp/postdownload?source=d ...

  3. windows上传文件到 linux的hdfs

    一.windows上传文件到 linux的hdfs 1.先在 centos 上开启 hdfs, 用 jps 可以看到下面信息, 说明完成开启 2.在win上配置 hadoop (https://www ...

  4. SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案

    SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOEx ...

  5. SpringBoot上传文件到本服务器 目录与jar包同级问题

    目录 前言 原因 实现 不要忘记 最后的封装 Follow up   前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了.当你使用tomcat发布项目的时候,上传 ...

  6. 【Azure 应用服务】使用PowerShell脚本上传文件至App Service目录  

    问题描述 使用PowerShell脚本上传文件至App Service目录的示例 脚本示例 对文件进行上传,使用的 WebClient.UploadFile 方法进行上传.当文件夹中包含子目录,执行以 ...

  7. python requests上传文件 tornado 接收文件

    requests 上传文件 import requests def images(): url = 'http://127.0.0.1:8889/upload/image' files = {'fil ...

  8. 使用windows桌面ftp上传文件到linux服务器

    首先在linux服务器上安装ftp [root@host2 test]#yum -y install ftp vsftpd [root@host2 test]#service vsftpd start ...

  9. 用sftp上传文件至linux服务器

    1.项目环境 框架:springmvc    项目管理工具:maven 2.必须使用的jar com.jcraft jsch 0.1.27 test 3.新建一个FileUpDown工具类,在类中添加 ...

随机推荐

  1. Android Tween和Frame 动画

    关于动画的实现,Android提供了Animation,在Android SDK介绍了2种Animation模式: 1. Tween Animation:通过对场景里的对象不断做图像变换(平移.缩放. ...

  2. Mac OS X 常用快捷键一览

  3. windows 下的 Apache 二级域名 目录绑定配置

    通常我们注册的域名都是顶级域名  如 www.potatog.com,我们希望这个域名可以访问服务器的不同网站或者不同功能等等 可能会这样 www.potatog.com/api 或者 www.pot ...

  4. mybatis-generator1.3.6的使用

    下载地址: http://blog.mybatis.org/2017/12/mybatis-generator-version-136-released.html 参考了 http://blog.cs ...

  5. How do I cover the “no results” text in UISearchDisplayController's searchResultTableView?

    How do I cover the "no results" text in UISearchDisplayController's searchResultTableView? ...

  6. iptables 详细使用

    检查状态 先检查是否安装了iptables $ service iptables status 安装iptables $ yum install iptables 升级iptables $ yum u ...

  7. Getting started with the basics of programming exercises_5

    1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...

  8. iPython的安装过程

    http://blog.csdn.net/u012587561/article/details/50900781 python2.7.10 amd64 win10 x64 1. 安装setuptool ...

  9. 给radio添加点击事件

    1.单独给每个radio添加点击事件 <fieldset id="form-gra-time"> <legend>请选择日期粒度:</legend&g ...

  10. HTML5的5个的新特性

    特性1:正则表达式 无须使用服务端的检测,使用浏览器的本地功能,就可以帮助你判断邮箱的格式,URL,电话,防止用户输入错误的信息,通过H5的pattern属性,很方便的整合这个功能. <inpu ...