简体   繁体   English

Python 使用 pexpect 通过 scp 发送文件

[英]Python sending files over scp using pexpect

So I'm trying to send all my torrent files to a server with scp using pexpect.所以我正在尝试使用 pexpect 将我所有的 torrent 文件发送到带有 scp 的服务器。 This works for the most part but not all my torrent files get sent.这在大多数情况下有效,但并非我所有的 torrent 文件都被发送。 Even though the print function in 'if i==0' prints out all the torrents.即使 'if i==0' 中的打印功能打印出所有种子。 And if I go to the server and delete everything that got sent and run it again, the same torrent files get sent.如果我转到服务器并删除发送的所有内容并再次运行它,则会发送相同的 torrent 文件。 Does anyone know what the problem might be?有谁知道问题可能是什么?

import os
import getpass
import subprocess
import pexpect

filepath = 'my downloads dir'

os.chdir(filepath)

var_password = str(getpass.getpass())
destination = "user@server:dir"

for filename in os.listdir():
    if filename.endswith('.torrent'):
        try:
            var_command = "scp " + filepath + filename + " " + destination

            var_child = pexpect.spawn(var_command)
            i = var_child.expect(["password:", pexpect.EOF])

            if i==0: # send password            
                print('Sending: ' + filename)    
                var_child.sendline(var_password)
                var_child.expect(pexpect.EOF)
            elif i==1: 
                print("Got the key or connection timeout")
                pass
        except Exception as e:
            print("Oops Something went wrong buddy")
            print(e)

I think I figured it out, thanks to David's comment.多亏了大卫的评论,我想我明白了。 The files that include spaces and icelandic letters (since I'm icelandic) don't get transferred.包含空格和冰岛字母(因为我是冰岛人)的文件不会被传输。

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

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