2017-06-04 38 views
0

我正在研究一个简短的本地(不推荐外部[非本机]模块,如pexpect),跨平台,不安全的远程控制应用程序的Python(Windows将使用py2exe和一个exe文件)。我正在使用start_new_thread来阻止呼叫,例如readline()。出于某种原因,但是,我得到丑陋的这个字符串作为我的输出:子进程Popen.stdin.write导致AttributeError

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):

#!/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,()) 

感谢您的帮助。

+0

'pipe'不能'None'。也许你可以删除插座的东西来创建一个简单的[mcve] –

+0

是的,这是建议'pipe'是None。为创建的地方插入测试;如果在那里有效,在线程函数中插入测试 –

+0

肯定会抛出错误的__exact__代码?我读过'subprocess.Popen'构造函数,它会在出错时引发异常,但不返回'None'。 – CristiFati

回答

0

事实证明,问题在于程序试图在两个start_new_thread调用之后退出,因为它已经到达最后,并在尝试这样做时导致错误。所以我代替:

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

有了:

start_new_thread(send_stream,()) 
read_stream() 
相关问题