python实现上传文件到linux指定目录的方法
作者:番茄小子 时间:2023-02-05 03:03:56
今天接到一个小需求,就是想在windows环境下,上传压缩文件到linux指定的目录位置并且解压出来,然后我想了一下,这个可以用python试试写下。
环境:
1.linux操作系统一台
2.windows环境安装Python3.x
3.Pycharm IDE环境
大家如果对paramiko模块不熟悉的话,可以去了解哈。
上传文件用到SFTPCLient,SFTPCLient作为一个sftp的客户端对象,根据ssh传输协议的sftp会话,实现远程文件操作,如上传、下载、权限、状态。
代码如下
#!/usr/bin/env python
import paramiko
import sys
src_file = sys.argv[1]#windows下的源文件
dsc_path = sys.argv[2]#linux上的目录路径
key_path = '私钥路径'
password = '密码'
ssh = paramiko.SSHClient() # 创建SSH对象
private_key = paramiko.RSAKey.from_private_key_file(key_path) # 指定私钥
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 允许连接不在know_hosts文件中的主机
transport = paramiko.Transport(('ip', 端口号))
transport.connect(username='root', pkey=private_key) # 连接服务器
def upload():
sftp = paramiko.SFTPClient.from_transport(transport)
try:
sftp.put(src_file, dsc_path + '/' + src_file)
print('上传成功')
except Exception as e:
print(e)
ssh.close()
def unzip():
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('cd ' + dsc_path + '&&' + 'unzip -o ' + src_file)
result = stdout.read().decode()
if result.strip() != '':
print('解压成功')
else:
print('Error message ' + stderr.read().decode())
ssh.close()
if __name__ == '__main__':
upload()
unzip()
可以分别通过Pycharm或者cmd来测试脚本运行,下面是用pycharm测试,把apps.zip文件上传到Linux服务器的/tmp目录下,并且解压
python upload_file.py apps.zip /tmp
上传成功
解压成功
然后登陆到Linux服务器的/tmp目录查看是否有压缩文件并且解压
总结 :这只是一个简单上传解压功能,请多多指教。
总结
以上所述是小编给大家介绍的python实现上传文件到linux指定目录的方法网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/summer-time/p/12142231.html
标签:python,上传文件,linux,指定,目录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解yii2使用多个数据库的案例
2024-05-13 09:53:07
Python安装Graphviz超详细图文教程
2022-11-13 05:50:26
![](https://img.aspxhome.com/file/2023/0/87480_0s.jpg)
python三引号如何输入
2021-08-12 12:42:34
mysql 8.0.13 安装配置方法图文教程
2024-01-14 15:20:52
![](https://img.aspxhome.com/file/2023/4/115294_0s.png)
利用Python Matlab绘制曲线图的简单实例
2021-05-16 07:21:38
![](https://img.aspxhome.com/file/2023/4/78494_0s.png)
PLSQL导入dmp文件的详细完整步骤
2023-06-26 11:45:32
![](https://img.aspxhome.com/file/2023/6/63716_0s.png)
python3新特性函数注释Function Annotations用法分析
2023-08-09 20:15:29
ChatGPT教你用Python实现BinarySearchTree详解
2023-11-18 15:26:18
![](https://img.aspxhome.com/file/2023/5/97585_0s.png)
基于python的selenium两种文件上传操作实现详解
2022-01-31 23:02:17
![](https://img.aspxhome.com/file/2023/6/97666_0s.png)
仿vue-cli搭建属于自己的脚手架的方法步骤
2024-05-21 10:18:09
浏览器常用基本操作之python3+selenium4自动化测试(基础篇3)
2023-10-19 04:49:03
![](https://img.aspxhome.com/file/2023/8/100808_0s.png)
正则表达式字面量在ECMAScript5中的变化
2012-04-26 16:23:16
C#实现按数据库邮件列表发送邮件的方法
2024-01-24 02:37:32
Python学习之configparser模块的使用详解
2022-07-21 23:21:25
![](https://img.aspxhome.com/file/2023/0/66380_0s.png)
你知道mysql哪些查询情况不走索引吗
2024-01-15 06:32:00
解决出现SoapFault (looks like we got no XML document)的问题
2023-11-19 04:21:01
python超详细实现完整学生成绩管理系统
2022-08-25 08:59:08
![](https://img.aspxhome.com/file/2023/5/82775_0s.png)
Mysql导入导出时遇到的问题解决
2024-01-22 02:28:06
![](https://img.aspxhome.com/file/2023/2/106602_0s.jpg)
php中关于socket的系列函数总结
2023-11-17 13:57:35
获取服务器传来的数据 用JS去空格的正则表达式
2024-04-16 09:31:07