【问题标题】:Python sending files over scp using pexpectPython使用pexpect通过scp发送文件
【发布时间】:2017-02-26 13:14:15
【问题描述】:

所以我正在尝试使用 pexpect 将我的所有 torrent 文件发送到带有 scp 的服务器。这在大多数情况下都有效,但并不是我所有的 torrent 文件都会被发送。即使 'if i==0' 中的打印功能会打印出所有种子。如果我去服务器并删除发送的所有内容并再次运行它,则会发送相同的 torrent 文件。有谁知道可能是什么问题?

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)

【问题讨论】:

  • 为什么不创建*.torrent 文件的存档,然后使用scp 发送呢?另外,脚本中的 scp 命令在哪里? var_command ?
  • 什么是var_command?此外,您可能正在超时连接,或者可能正在为服务器发送包含不兼容字符/空格的文件。
  • 在这里,抱歉我在重构代码时删除了它。但我正试图让它发挥作用,因为它将成为更大系统的一部分
  • 无论如何我都会压缩文件,将其发送到目标服务器,然后在那里解压。

标签: python operating-system scp pexpect torrent


【解决方案1】:

感谢大卫的评论,我想我明白了。包含空格和冰岛字母(因为我是冰岛人)的文件不会被传输。

【讨论】:

  • 根据你的用户名,我猜你是瑞典人。至少是右半球。
猜你喜欢
  • 2011-03-04
  • 2012-12-31
  • 2013-02-16
  • 1970-01-01
  • 2016-11-22
  • 2012-03-02
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
相关资源
最近更新 更多