.description _
The business has a delivery process, and the manual operation is too cumbersome, so I want to implement it through code. The first problem is to interact with the linux server on Windows, upload and download files using the SFTP protocol (SSL-encrypted FTP protocol, similar to HTTPS. PS: Personal understanding!)
Install
pip install paramiko
code demo
import paramiko
host = "123.123.123.123"
port = 54321
user = "XXX"
password = "XXXX"
# 第一种登录服务器的方法
def login_sftp1():
try:
# 建立连接管道
t = paramiko.Transport((host,port))# 注意是双层括号,之前搞了好久。
# 建立连接
t.connect(username=user,password=password)
# 实例化一个clint对象,并通过ssh transport操作文件
sftp = paramiko.SFTPClient.from_transport(t)
except Exception as e:
print (e)
# 查看目标服务器的当前文件夹的目录文件,默认参数path='.'。
print sftp.listdir()
t.close()
sftp.close()
# 第二种登录方法
def login_sftp2()
try:
sc= paramiko.SSHClient()
sc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sc.connect(host,port,user,password)
# 获取操作文件的实例也有两种方式
sftp = paramiko.SFTPClient.from_transport(sc.get_transport())#①
-------------------------------------------------------------------
sftp = ssh.open_sftp()#②
except Exception as e:
print (e)
print sftp.listdir()
sc.close()
sftp.close()
----------
# 文件上传
sftp.put(localpath,remotepath)
#文件下载
sftp.get(remotepath,localpath)