繁体   English   中英

Python 将文件从远程机器移动到本地

[英]Python Move a File from Remote Machine to Local

我是 python 的新人,正在尝试不同的东西。

目前正在尝试将文本文件to_copy.txt从本地 ip 192.168.1.101的远程机器复制到我当前的机器。

我从谷歌搜索中尝试的方法似乎不起作用。

import paramiko
from scp import SCPClient

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("testme@192.168.1.101", password="look420")
print("Connected")
scp = SCPClient(ssh.get_transport())
scp.get("/home/testme/target_folder/to_copy.txt")
scp.close()

但是,当我运行它时出现错误;

Traceback (most recent call last):
  File "/home/uc/Python_Projects/MoveFileAndFolder/move_remote.py", line 7, in <module>
    ssh.connect("testme@192.168.1.101", password="look420")
  File "/usr/local/lib/python3.4/dist-packages/paramiko/client.py", line 296, in connect
    to_try = list(self._families_and_addresses(hostname, port))
  File "/usr/local/lib/python3.4/dist-packages/paramiko/client.py", line 200, in _families_and_addresses
    addrinfos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
  File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

我在这里做错了什么?

注意:当前机器运行Debian Jessie ,远程机器运行Ubuntu 14.04.4 LTS

scp(22)的端口可能在远程计算机上未打开。 请通过命令行调用进行检查,以确认您确实可以建立ssh或scp连接。

看到这里更多细节

https://help.ubuntu.com/community/SSH/TransferFiles

你试过了吗

ssh.connect("192.168.1.101", username="testme", password="look420")

请参考文件

通过paramiko从远程服务器下载文件资源到本地

import paramiko
import os
from stat import S_ISDIR as isdir


def down_from_remote(sftp_obj, remote_dir_name, local_dir_name):
    "" "download files remotely" ""
    remote_file = sftp_obj.stat(remote_dir_name)
    if isdir(remote_file.st_mode):
        # Folder, can't download directly, need to continue cycling
        check_local_dir(local_dir_name)
        print('Start downloading folder: ' + remote_dir_name)

        for remote_file_name in sftp.listdir(remote_dir_name):
            sub_remote = os.path.join(remote_dir_name, remote_file_name)
            sub_remote = sub_remote.replace('\\', '/')
            sub_local = os.path.join(local_dir_name, remote_file_name)
            sub_local = sub_local.replace('\\', '/')
            down_from_remote(sftp_obj, sub_remote, sub_local)
    else:
        # Files, downloading directly
        print('Start downloading file: ' + remote_dir_name)
        sftp.get(remote_dir_name, local_dir_name)


def check_local_dir(local_dir_name):
    "" "whether the local folder exists, create if it does not exist" ""
    if not os.path.exists(local_dir_name):
        os.makedirs(local_dir_name)


if __name__ == "__main__":
    "" "program main entry" ""
    # Server connection information
    host_name = 'ipaddress'
    user_name = 'username'
    password = 'password'
    port = 22
    # Remote file path (absolute path required)
    remote_dir = '/data/nfs/zdlh/pdf/2018/07/31'
    # Local file storage path (either absolute or relative)
    local_dir = 'file_download/'

    # Connect to remote server
    t = paramiko.Transport((host_name, port))
    t.connect(username=user_name, password=password)
    sftp = paramiko.SFTPClient.from_transport(t)

    # Remote file start download
    down_from_remote(sftp, remote_dir, local_dir)

    # Close connection
    t.close()

""通过paramiko从远程服务器下载文件资源到本地作者:gxcuizy 时间:2018-08-01"""

import paramiko import os from stat import S_ISDIR as isdir

def down_from_remote(sftp_obj, remote_dir_name, local_dir_name): "" "远程下载文件" "" remote_file = sftp_obj.stat(remote_dir_name) if isdir(remote_file.st_mode): # 文件夹,不能直接下载,需要继续循环 check_local_dir( local_dir_name) print('开始下载文件夹:' + remote_dir_name)

    for remote_file_name in sftp.listdir(remote_dir_name):
        sub_remote = os.path.join(remote_dir_name, remote_file_name)
        sub_remote = sub_remote.replace('\\', '/')
        sub_local = os.path.join(local_dir_name, remote_file_name)
        sub_local = sub_local.replace('\\', '/')
        down_from_remote(sftp_obj, sub_remote, sub_local)
else:
    # Files, downloading directly
    print('Start downloading file: ' + remote_dir_name)
    sftp.get(remote_dir_name, local_dir_name)

def check_local_dir(local_dir_name): "" "本地文件夹是否存在,不存在则创建" "" if not os.path.exists(local_dir_name): os.makedirs(local_dir_name)

if name == " main ": "" "程序主入口" "" # 服务器连接信息 host_name = 'ip' user_name = 'name' password = 'pwd' port = 22 # 远程文件路径(需要绝对路径) remote_dir = '/data/nfs/zdlh/pdf/2018/07/31' # 本地文件存放路径(绝对或相对) local_dir = 'file_download/'

# Connect to remote server
t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)

# Remote file start download
down_from_remote(sftp, remote_dir, local_dir)

# Close connection
t.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM