在Python中,如何将字符串写入远程计算机上的文件?

时间:2013-10-05 20:21:03

标签: python file ssh network-programming rsync

在Machine1上,我有一个Python2.7脚本,它在RAM中计算一个大的(最多10MB)二进制字符串,我想写入Machine2(一台远程机器)上的磁盘文件。做这个的最好方式是什么?

约束:

  • 这两台机器都是Ubuntu 13.04。他们之间的联系很快 - 他们在同一个网络上。

  • 目标目录可能尚未存在于Machine2上,因此可能需要创建目标目录。

  • 如果这很容易,我想避免将RAM中的字符串写入Machine1上的临时磁盘文件。这是否会消除可能使用系统调用rsync的解决方案?

  • 因为字符串是二进制的,所以它可能包含可以解释为换行符的字节。这似乎排除了可能在Machine2上使用对echo命令的系统调用的解决方案。

  • 我希望这在Machine2上尽可能轻量级。因此,我想避免在Machine2上运行ftp等服务或在那里进行其他配置活动。另外,我不太了解安全性,因此除非真的有必要,否则我们希望避免打开其他端口。

  • 我在Machine1和Machine2上设置了ssh密钥,并希望将它们用于身份验证。

  • 编辑:Machine1正在运行多个线程,因此多个线程可能会在重叠时间尝试写入Machine2上的同一文件。我不介意在这种情况下将文件写入两次(或更多)所导致的低效率,但是Machine2上的结果数据文件不应该被同时写入损坏。也许需要在Machine2上锁定操作系统?

我正在寻找一个rsync解决方案,因为它是一个我自己理解得很好的自包含实体,并且不需要在Machine2上进行配置。

6 个答案:

答案 0 :(得分:13)

Paramiko支持在远程计算机上打开文件:

import paramiko

def put_file(machinename, username, dirname, filename, data):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(machinename, username=username)
    sftp = ssh.open_sftp()
    try:
        sftp.mkdir(dirname)
    except IOError:
        pass
    f = sftp.open(dirname + '/' + filename, 'w')
    f.write(data)
    f.close()
    ssh.close()


data = 'This is arbitrary data\n'.encode('ascii')
put_file('v13', 'rob', '/tmp/dir', 'file.bin', data)

答案 1 :(得分:6)

使用subprocess.Popen打开到Machine2的新SSH进程,然后将数据写入STDIN。

import subprocess

cmd = ['ssh', 'user@machine2',
       'mkdir -p output/dir; cat - > output/dir/file.dat']

p = subprocess.Popen(cmd, stdin=subprocess.PIPE)

your_inmem_data = 'foobarbaz\0' * 1024 * 1024

for chunk_ix in range(0, len(your_inmem_data), 1024):
    chunk = your_inmem_data[chunk_ix:chunk_ix + 1024]
    p.stdin.write(chunk)

我刚刚确认它的工作方式与广告一样,并复制了所有10485760个虚拟字节。

PS 一个可能更清晰/更优雅的解决方案是让Python程序将其输出写入sys.stdout,并在外部进行管道{/ 1}}:

ssh

答案 2 :(得分:1)

对@Erik Kaplun 的回答稍作修改,以下代码对我有用。 (使用communication() 而不是.stdin.write)

import subprocess
# convert data to compatible format
cmd = ['ssh', 'user@machine2', 'cat - > /path/filename']
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
p.communicate(data)

答案 3 :(得分:0)

如果只是调用子进程,那么可能是sh.py 可能是正确的事情。

from sh import ssh
remote_host = ssh.bake(<remote host>) 
remote_host.dd(_in = <your binary string>, of=<output filename on remote host>) 

答案 4 :(得分:0)

您未通过某种连接明确发送数据的解决方案是使用sshfs。您可以使用它在Machine1上的某个位置从Machine2挂载目录,并且写入该目录中的文件将自动导致数据写入Machine2。

答案 5 :(得分:0)

我们可以通过三个简单的步骤将字符串写入远程文件:

  1. stringtemp file
  2. temp file复制到remote host
  3. 删除 temp file

这是我的代码(没有任何第三方

import os

content = 'sample text'
remote_host = 'your-remote-host'
remote_file = 'remote_file.txt'

# step 1
tmp_file = 'tmp_file.txt'
open(tmp_file, 'w').write(content)

# step 2
command = 'scp %s %s:%s' % (tmp_file, remote_host, remote_file)
os.system(command)

# step 3
os.remove(tmp_file)