需要用到paramiko库
上传本地文件到服务器。将代码中的ip,port,username,password补充完整,运用其中uploadfiletoserver函数即可上传本地文件到服务器
import paramiko
ip = ""#服务器ip
port = 22#端口号
username = "root"#用户名
password = ""#密码
def uploadfiletoserver(local,remote):#上传文件到服务器.local是要上传文件的本地路径;remote是上传到服务器的路径
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
sftp = ssh.open_sftp()
sftp.put(local, remote)
return remote
远程打开服务器文件:
import paramiko
ip = ""#服务器ip
port = 22#端口号
username = "root"#用户名
password = ""#密码
def openremotefile(filepath):#filepath是服务器上要打开的文件的绝对路径
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, port, username, password, compress=True)
sftp_client = client.open_sftp()
remotefile = sftp_client.open(filepath) # 文件路径
return remotefile
所有代码
import paramiko
ip = ""#服务器ip
port = 22#端口号
username = "root"#用户名
password = ""#密码
def uploadfiletoserver(local,remote):#上传文件到服务器.local是要上传文件的本地路径;remote是上传到服务器的路径
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
sftp = ssh.open_sftp()
sftp.put(local, remote)
return remote
def openremotefile(filepath):#filepath是服务器上要打开的文件的绝对路径
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, port, username, password, compress=True)
sftp_client = client.open_sftp()
remotefile = sftp_client.open(filepath) # 文件路径
return remotefile