2016-08-01 8 views
1

sshを介して大きなファイルをストリーミングしようとしています。以下のように:pythonで "open()as f:"のようなコマンドからstdoutを処理する方法

with open('somefile','r') as f: 
    tx.send(filepath='somefile',stream=f.read()) 

私はそれがうまくこの方法をスト​​リーミングすることができますが、私は同様にストリーミングするpvdd、およびtarのようなコマンドを使用できるようにする必要があり、より高いレベルのクラスのインスタンスであるTX。私は必要なのである。

with run_some_command('tar cfv - somefile') as f: 
    tx.send(filepath='somefile',stream=f.read()) 

これはストリームとしてSTDOUTを取り、リモートのファイルに書き込みます。私は例を見つけようとしばらくグーグルませんが、今のところ何の運きた ...

p = subprocess.Popen(['tar','cfv','-','somefile'], stdout=subprocess.PIPE) 
tx.send(filepath='somefile',stream=p.stdout.readall()) 

が、無駄に: は、私のような何かを試してみました。 ご迷惑をおかけして申し訳ございません。

答えて

0

唯一の問題は存在しない.readall()メソッドだと思います。私は戻って歩いて、基本的な例を始め

p = subprocess.Popen(['tar','cfv','-','somefile'], stdout=subprocess.PIPE) 
tx.send(filepath='somefile',stream=p.stdout.read()) 
0

calc_table_file = '/mnt/condor/proteinlab/1468300008.table' 

import subprocess 
class TarStream: 
    def open(self,filepath): 
    p = subprocess.Popen(['tar','cfv','-',filepath], stdout=subprocess.PIPE) 
    return(p.stdout) 


import paramiko 


def writer(stream): 
    ssh = paramiko.SSHClient() 
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    ssh.connect('asu-bulk-uspacific', username='labtech', password=password) 
    client = ssh.open_sftp() 
    with client.open('/mnt/cold_storage/folding.table','w') as f: 
    while True: 
     data = stream.read(32) 
     if not data: 
     break 
     f.write(data) 

## works with normal 'open' 
with open(calc_table_file,'r') as f: 
    writer(f) 

## and popen :) 
tar = TarStream() 
writer(tar.open(calc_table_file)) 

をし、それが働いた

あなたは標準出力の内容全体を読み取るためにp.stdout.read()を使用することができます!助けてくれてありがとう。

関連する問題