diff -ur v5/chat_conn.py v6/chat_conn.py --- v5/chat_conn.py Sat Aug 31 14:18:00 2019 +++ v6/chat_conn.py Sun Sep 1 15:30:00 2019 @@ -5,6 +5,8 @@ import select import time import threading +import os +import signal import subprocess try: import queue @@ -413,7 +415,30 @@ th.join() cli.dis_conn() +def sproc_new(cmd): + proc = None + if cmd: + try: + PIPE = subprocess.PIPE + proc = subprocess.Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, preexec_fn=os.setsid) + except: + pass + + (stdin, stdout) = (proc.stdin, proc.stdout) if proc else (None, None) + + def kill(): + if proc: + proc.poll() + if proc.returncode == None: + os.killpg(proc.pid, signal.SIGKILL) + + return Empty( locals() ) + def cmd_io_new(cmd): + k = 'cont:' + cont_f = cmd.startswith(k) + if cont_f: + cmd = cmd[len(k):] k = 'me:' me_f = cmd.startswith(k) if me_f: @@ -421,15 +446,20 @@ que = queue.Queue() + sproc = sproc_new(cmd) if cont_f and cmd else None + def func_in(): s = None - if cmd: + f = sproc.stdout if sproc else sys.stdin + + if cmd and sproc == None: try: s = que.get(timeout=tmout) except: pass - elif readable(sys.stdin): - s = sys.stdin.readline().strip() + elif f and readable(f): + s = f.readline().strip() + # print('func_in {}'.format(s)) # dbg return s def cmd_exec(s, cmd): @@ -439,22 +469,27 @@ que.put(ret) def func_out(f, s, name): - if cmd: - if me_f: - k = ' >' + name - if s.endswith(k): - cmd_exec( s[:-len(k)], cmd ) - else: - cmd_exec( '{}> {}'.format(f, s), cmd ) + if me_f: + k = ' >' + name + if not s.endswith(k): + return + s = s[:-len(k)] else: - print( '{}> {}'.format(f, s) ) + s = '{}> {}'.format(f, s) + f = sproc.stdin if sproc else sys.stdout + if cmd and sproc == None: + cmd_exec(s, cmd) + elif f: + f.write(s+'\n') + f.flush() + # print('func_out {}'.format(s)) # dbg return Empty( locals() ) def help(v): ss = [ 'Usage: {} [conn [] [host=] \\'.format(sys.argv[0]), - ' [cmd=[me:]]]', + ' [cmd=[cont:][me:]]]', ' conn: client mode', ' user name: default "guest"', ' hostname: default "localhost"',