python打开本地服务器文件,如何用python传输本地文件到服务器/远程打开服务器上的文件...

需要用到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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值