-1

if i set command1 = "start notepad.exe" is there a way to make the script output gino.stdout and gino.stderr without waiting for notepad.exe to be closed?

import socket
import subprocess
import os

HOST = '//' # 
PORT = 8081 # 
server = socket.socket()
server.bind((HOST, PORT))
# print('[+] Server Started')
# print('[+] Listening For Client Connection ...')
server.listen(1)
client, client_addr = server.accept()
# print(f'[+] {client_addr} Client connected to the server')

while True:
    command = client.recv(4096)
    command1 = command.decode()
    print(command1)
    if command1 != "exit":
       gino = subprocess.run(command1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
       risposta = gino.stdout + gino.stderr
       if risposta != b"":
          client.send(risposta)
          print(risposta)
       else:
          v = "Executed " + command1
          print(v)
          client.send(v.encode())
    else:
       client.close()
       sys.exit()
Narrow
  • 1
  • 3

1 Answers1

0

To my surprise this is somewhat harder than I expected. I'm assuming notepad.exe is your command, and I'm assuming you're on windows, so tools like stdbuf are not available.

Getting the output from a program which flushes stdout is easy:

# tesflush.py
import time
from sys import stdout

for i in range(100):
    print(f"line {i}")
    stdout.flush()
    time.sleep(0.1)
import subprocess
from time import sleep

p = subprocess.Popen(
    ["python", "testflush.py"], stdout=subprocess.PIPE, encoding="utf8"
)

for line in iter(p.stdout.readline, ""):
    print(line, end="")

But if the application does not flush stdout, life is a little harder. I ended up stumbling on this question which mentions using pexpect as a simple way to get a pseudo-tty and thus force the output to flush:

#testnoflush.py
import time

for i in range(100):
    print(f"line {i}")
    time.sleep(0.1)
import pexpect

child = pexpect.spawn("python test2.py", encoding="utf8")
for line in child:
    print(line, end="")
child.close()

I suspect you will need something like this (the linked question has a manual implementation if you really need to avoid pexpect.)

2e0byo
  • 5,305
  • 1
  • 6
  • 26