【问题标题】:Remote to remote large file copy with python使用python远程到远程大文件复制
【发布时间】:2021-03-25 15:48:59
【问题描述】:

我正在编写其他人的 Python 脚本,该脚本在两个网络位置之间传输大文件(700mb 以上)。该脚本当前使用 os.rename 和 shutil 来复制这些文件。我是 python 新手,但我知道这是将文件从位置 a 复制到我的本地 Windows 机器,然后复制到位置 b。这相当慢,我已经开始寻找一种使复制速度更快的方法,我认为这将涉及不复制到本地机器,而是直接在位置 a 和 b 之间复制。

有没有人知道如何以 Python 方式做到这一点?

【问题讨论】:

    标签: python windows ssh scp


    【解决方案1】:

    sendfile.py = (connect\socket)

    import os
    import socket
    
    total = []
    directory = "C:\\folder\\"
    files = os.listdir(directory)
    for file in files:
        sock = socket.socket()
        host = "192.168.1.111"
        port = 667
        sock.connect((host, port))
        total += file.split(',')
        sock.send(str.encode(file))
        file = str.encode(directory + file)
        with open(file, "rb") as file:
            data = file.read(1024)
            resp = sock.recv(1024)
            if resp == b'Wainting file':
                file_size = os.path.getsize(arquivo)
                sock.send(bytes(str(file_size), 'utf-8'))
                if resposta == b'Waiting...':
                    while data:
                        sock.send(data)
                        data = file.read(1024)
                        if not data:
                            sock.close()
                    if total == files:
                        print(total)
    

    revice.py = (绑定/套接字)

    import socket
    
    sock = socket.socket()
    host = '192.168.1.111'
    port = 667
    sock.bind((host, port))
    sock.listen(10)
    total = []
    while True:
        conn, addr = socket.accept()
        directory = "C:\\folder\\to_recive\\"
        data = conn.recv(1024)
        while data == b'':
            conn.close()
            conn, addr = sock.accept()
            data = conn.recv(1024)
        total += data.decode().split(',')
        name_file = total[-1]
        with open(f"{directory}{name_file}", "wb") as file:
            conn.send(b"Waiting file")
            data = conn.recv(1024)
            size = data.decode('utf-8')
            file_size = int(size)
            if file_size != 0:
                conn.send(b'Waiting...')
                print("HOST <--> " + addr[0] + f" Download {name_file} - {size} kb")
                while data:
                    data = conn.recv(1024)
                    file.write(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2013-06-01
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多