用python实现上传文件到服务器

# -*- coding: utf-8 -*-
# @Time    : 2022/3/16 10:43
# @Author  : hjcui
# @Site    : 
# @File    : upload.py
# @Software: PyCharm

import paramiko
from scp import SCPClient


if __name__ == '__main__':
    host = "192.17.52.152"
    port = 22
    username = "root"
    password = "123456"

    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    ssh_client.connect(host, port, username, password)
    scpclient = SCPClient(ssh_client.get_transport(), socket_timeout=15.0)

    wav_dir = "D:/swk_wewr"
    with open("./swk_wewr_3min.txt", "r", encoding="utf-8") as reader:
        for i, row in enumerate(reader):
            speaker_id = row.strip()
            wav_files = os.listdir(os.path.join(wav_dir, speaker_id))
            for file in wav_files:
                file_path = os.path.join(wav_dir, speaker_id, file)
                try:
                    scpclient.put(file_path, "/home/data/" + speaker_id)
                    print(i, "文件上传成功")
                except:
                    print("文件上传失败")

你可能感兴趣的:(python)