2016-12-05 2 views
0

제가하려는 것은 CMD 명령을 실행할 수있는 서버와 클라이언트를 만드는 것입니다.파이썬을 사용하여 CMD 명령을 실행하십시오.

저는 서버 - 클라이언트 통신을 관리했지만 파이썬을 사용하여 명령 프롬프트를 제어 할 때 문제가 있습니다.

내 현재 코드는 다음 코드와

import time 
import _thread 
import winpexpect 
class CommandPrompt(object): 
    def __init__(self): 
     self.cmd = winpexpect.winspawn('cmd.exe') 
    def read(self): 
     while True: 
      if self.cmd.readline(): 
       print(self.cmd.before) 
    def send(self,usinput): 
     self.cmd.sendline(usinput) 
    def close(self): 
     self.cmd.close() 
cmd = CommandPrompt() 
_thread.start_new_thread(cmd.read,()) 
time.sleep(1) 
while True: 
    cmd.send(input('>>')) 

난 쉘 명령을 실행할 수 있어요하지만 항상 내가 마지막 줄, 현재의 경로를 보여주는 내 입력 ex:C:\Windows\system32>을 기다리는 일이 없습니다. 그 라인이 보이지 않는 이유는 그것이 입력되지 않았기 때문이라고 생각합니다.

또한 어떤 명령을 입력하지 않은 시간이 지나면 pexpect.TIMEOUT이 작동을 멈추고 시간 초과가 발생해도 문제를 해결할 수 있지만 읽는 방법에 결함이 있다는 사실은 변하지 않습니다.

누구든지 명령 프롬프트의 출력을 읽을 수 있습니까? 나는 충분한이 문제를 설명하지 않은 경우

가 미안 해요, 당신은에 파이프 출력을 시도 할 수 유래 : 에 여기에 도움을 요청하는 내 처음으로 ...

답변

0

입니다 출력 파일을 읽고 그 파일을 읽습니다. 예 :

import time 
import _thread 
import winpexpect 
class CommandPrompt(object): 
    def __init__(self): 
     self.cmd = winpexpect.winspawn('cmd.exe') 
    def read(self): 
     while True: 
      if self.cmd.readline(): 
       print(self.cmd.before) 
    def send(self,usinput): 
     self.cmd.sendline(usinput) 
    def close(self): 
     self.cmd.close() 
cmd = CommandPrompt() 
_thread.start_new_thread(cmd.read,()) 
time.sleep(1) 
while True: 
    cmd.send(input('>>') + " > out.txt") 

    # some sort of function to request the contents of "out.txt" 

    # some sort of function to remove "out.txt" 
+0

예.하지만 os.system에서 명령을 실행할 때마다 다른 cmd 인스턴스에서 실행됩니다. 나는 모든 명령이 동일한 cmd 인스턴스에서 실행되어야하므로 "cd foo", "dir/d"와 같은 작업을 수행 할 수 있어야합니다. 빠른 응답을 주셔서 감사합니다. –

+0

@PopescuIonutAlexandru 걱정할 필요가 없습니다! 그에 따라 게시물을 편집합니다. –

관련 문제