:param remote_file: 要上传的文件地址(例:/test.txt)
:return:
"""
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, user, password)
sftp = ssh.open_sftp()
sftp.put(local_file, remote_file)
具体的参数解释如上代码所示,直接运行即可将本地文件上传到服务器端
案例:**上传文件到本地,然后再分发到服务器上**
def test(request):
if request.method == “POST”:
file = request.FILES.get(“file”)
# BASE_DIR为项目路径,path为要保存文件路径
path = os.path.join(BASE_DIR, ‘begin\static’, ‘file’, file.name)
# 二进制的方式写入文件
with open(path, ‘wb’) as f:
for i in file.chunks():
f.write(i)
ssh_scp_put(‘114.116.100.185’, 22, ‘root’, ‘*****’, path, ‘/{}’.format(file.name))
return render(request, ‘operation.html’)
### ps:paramiko使用出现CryptographyDeprecationWarning: encode\_point has been deprecated on Ellipti