Skip to content Skip to sidebar Skip to footer

Execute Cmd Commands Using Python

what i'm trying to do is create a server and a client, the server being able to execute CMD commands. I managed to do the server-client communication but i have problems at control

Solution 1:

You could try to pipe the output into an output file and read that file. For example:

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"

Post a Comment for "Execute Cmd Commands Using Python"