将指定目录的文件上传到服务器指定目录
直接上代码:
import paramiko # 用于调用scp命令
from scp import SCPClient
# remote_path远程服务器目录
# file_path本地文件夹路径
# img_name是file_path本地文件夹路径下面的文件名称
def upload_server(ip,username,password,img_name, remote_path, file_path):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh_client.connect(hostname=str(ip), port=22, username=username, password=password)
scpclient = SCPClient(ssh_client.get_transport(),socket_timeout=15.0)
local_path = file_path + "\\" + img_name
try:
scpclient.put(local_path, remote_path)
except FileNotFoundError as e:
print(e)
print("系统找不到指定文件" + local_path)
else:
print("文件上传成功")
ssh_client.close()
def upload_server_run():
host = "0.0.0.0" #服务器ip地址
port = 22 # 端口号
username = "admin" # ssh 用户名
password = "admin" # 密码
img_name = "1.jpg" #是file_path本地文件夹路径下面的文件名称
file_path = "D:\" #本地文件夹路径
remote_path = "/home" #远程服务器目录
upload_server(ip,username,password,img_name, remote_path, file_path)
if __name__ =='__main__':
thread = Thread(target =upload_server_run)
thread.start()