Skip to content

Instantly share code, notes, and snippets.

@Jinmo
Last active April 16, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jinmo/ea5b7fe6f3b5acc0f5bd06f45a3bc9ef to your computer and use it in GitHub Desktop.
Save Jinmo/ea5b7fe6f3b5acc0f5bd06f45a3bc9ef to your computer and use it in GitHub Desktop.
child process console realtime output in IDAPython
import subprocess
import Queue
import threading
import time
import sys
def Popen(*args, **kwargs):
q = Queue.Queue()
done = []
def receiver_thread():
buff = []
last_output_time = time.time()
while not (done and q.empty()):
cur_time = time.time()
if last_output_time < cur_time - 0.01:
sys.stdout.write(''.join(buff).replace('\r', ''))
last_output_time = cur_time
buff[:] = []
try:
item = q.get(timeout=0.01)
except Queue.Empty:
continue
buff.append(item)
q.task_done()
sys.stdout.write(''.join(buff).replace('\r', ''))
def reader_thread():
while True:
c = p.stdout.read(1)
if not c:
done.append(True)
break
q.put(c)
if 'stdout' not in kwargs:
kwargs['stdout'] = subprocess.PIPE
if 'stderr' not in kwargs:
kwargs['stderr'] = subprocess.STDOUT
p = subprocess.Popen(*args, **kwargs)
t1 = threading.Thread(target=reader_thread)
t2 = threading.Thread(target=receiver_thread)
t1.start()
t2.start()
return p
def system(cmd):
return Popen(cmd, shell=True)
Popen('pip install requests', shell=True)
system('pip install requests')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment