转载请注明出处:【博客园-clayyjh-https://www.cnblogs.com/clayyjh/p/14456453.html】
如果对您有帮助,可以给作者加个🍗哈!
本机为Windows电脑/Linux设备(例如树莓派),云端为Linux服务器,通过调用python的paramiko进行通信。
1. 边缘向云端发送数据文件,边缘端执行py文件,代码如下:
以Windows电脑向树莓派发送文件为例子
首先在Windows配置python3环境 python 3.8是可以的,树莓派系统自带Python3开发环境,无需安装。
首先在边缘和云端设备上安装paramiko库 cmd命令行或者Linux终端执行 pip3 install paramiko
import paramiko ip = "192.168.0.102" # 服务器ip port = 22 # 端口号 默认22 username = "pi" # 用户名 这个是树莓派的默认用户名 password = "raspberry" # 密码 这个是树莓派的默认密码 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 def downloadfiletoserver(remote, local): # 上传文件到服务器.local是要上传文件的本地路径;remote是上传到服务器的路径 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip, port, username, password) sftp = ssh.open_sftp() sftp.get(remote, local) return remote localpath = r'E:/raspberry/filetransfer/a.txt' remotepath = r'/home/pi/ec/c.txt' # localpath = localpath.strip("u202a") uploadfiletoserver(localpath, remotepath) #downloadfiletoserver(remotepath, localpath) print("hello")
2. 云端向边缘端发送数据文件
若边缘端是windows电脑,需要按照这个教程进行操作:https://www.cnblogs.com/clayyjh/p/14454417.html
云端向边缘发送文件和边缘向云端是一样的,只需要进行以下更改
ip 替换为Windows的ip
port 默认22
username Windows的用户
password Windows的密码
localpath 更改为云端服务器的路径
remotepath 更改为本地windows的路径
本文来自博客园,作者:Clay,转载请注明原文链接:https://www.cnblogs.com/clayyjh/p/14456453.html