Python实现远程文件传输

   日期:2020-10-10     浏览:168    评论:0    
核心提示:Python实现远程文件传输(socket) (paramiko)环境Python 3.7.4socketparamiko==2.7.1CentOS Linux release 8.1.1911 (Core)实现代码<host name>为远程计算机主机名(云服务器公网IP),<port>为端口号,云服务器默认为22,使用其他端口需要到安全组配置放开端口,<user name>为登录用户名,阿里云默认为root,<public key path>

Python实现远程文件传输(socket) (paramiko)

环境

Python 3.7.4
socket
paramiko==2.7.1
CentOS Linux release 8.1.1911 (Core)

实现代码

<host name>为远程计算机主机名(云服务器公网IP),<port>为端口号,云服务器默认为22,使用其他端口需要到安全组配置放开端口,<user name>为登录用户名,阿里云默认为root<public key path>为登录云服务器的密钥文件路径

import os
import paramiko

class SCPTransmitter():
	```
		Description:
			upload local files to remote server or download remote file to local dirctory by Secure Copy and SSH File Transfer Protocol
		Attributes:
			None
	```
    def __init__(self):

        self.hostname = '<host name>'
        self.port = <port>
        self.username = <user name>

        self.public_key = paramiko.RSAKey.from_private_key_file('<public key path>')
        self.scp = paramiko.Transport(self.hostname, self.port)
        self.scp.connect(username=self.username, pkey=self.public_key)
        self.sftp = paramiko.SFTPClient.from_transport(self.scp)

	```
		Description:
			download remote files from remote server
		Args:
			remote_path:
				remote dirctory of files wanna download
			local_path:
				local path to save files
		Returns:
			None
	```	

    def download(self, remote_path, local_path):
        try:
            remote_files = self.sftp.listdir(remote_path)
            for file in remote_files:
                local_file = local_path + file
                print(local_file)
                remote_file = remote_path + file
                print(remote_file)
                self.sftp.get(remote_file, local_file)
            print("Successfully")
        except IOError:
            return ("remote_path or local_path is not exist")

	```
		Description:
			upload local files to remote server
		Args:
			remote_path:
				remote dirctory to save files
			local_path:
				local directory of files wanna upload 
	```
    def upload(self, remote_path, local_path):
        try:
            local_files = os.listdir(local_path)
            print(local_files)
            for file in local_files:
                local_file = local_path + file
                print(local_file)
                remote_file = remote_path + file
                print(remote_file)
                self.sftp.put(local_file, remote_file)
            print("Successfully")
        except IOError:
            return ("remote_path or local_path is not exist")



if __name__ == "__main__":

    remote_path = '/home/remote/'    
    local_path = './res/remote/'
    transmitter = SCPTransmitter()
    transmitter.upload(remote_path, local_path)

最后

  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服