2021-04-13 20:06:16 +00:00
#!/usr/bin/env python3
import sys
oldexcepthook = sys . excepthook
def newexcepthook ( type , value , traceback ) :
oldexcepthook ( type , value , traceback )
2021-04-14 01:12:13 +00:00
#input("Press ENTER to quit.")
2021-04-13 20:06:16 +00:00
sys . excepthook = newexcepthook
import os
p = os . path . join
pUp = os . path . dirname
s = False
if getattr ( sys , ' frozen ' , False ) and hasattr ( sys , ' _MEIPASS ' ) :
s = os . path . realpath ( sys . executable )
else :
s = os . path . realpath ( __file__ )
sp = pUp ( s )
# script start
2021-04-13 18:33:16 +00:00
import subprocess
import socket
2021-04-14 15:06:44 +00:00
import threading
import queue
2021-04-13 20:06:16 +00:00
2021-04-20 14:32:56 +00:00
bufferSize = 10000 # buffer size in bytes
queueLengthWait = 10 # How many buffers can be in the queue before waiting? 0 for infinite, maxAccumulatedData comes into play. Raise for smoother playback, lower for less delay.
maxAccumulatedData = 50 * 1000 * 1000 # If queueLengthWait is 0, how much data can be in an outbound thread's queue at maximum before the connection is closed?
2021-04-14 16:52:18 +00:00
timeout = 15 # timeout in seconds
2021-04-13 18:33:16 +00:00
connection = socket . socket ( socket . AF_INET , socket . SOCK_STREAM )
2021-04-14 15:06:44 +00:00
class stdoutThread ( threading . Thread ) :
def __init__ ( self ) :
threading . Thread . __init__ ( self )
self . queue = queue . Queue ( )
def run ( self ) :
while True :
2021-04-14 16:52:18 +00:00
data = self . queue . get ( timeout = timeout )
2021-04-14 15:06:44 +00:00
sys . stdout . buffer . write ( data )
2021-04-14 20:03:53 +00:00
class stdinThread ( threading . Thread ) :
def __init__ ( self , connection ) :
threading . Thread . __init__ ( self )
2021-04-20 14:32:56 +00:00
self . queue = queue . Queue ( queueLengthWait )
2021-04-14 20:03:53 +00:00
self . connection = connection
def run ( self ) :
try :
while True :
accumulatedData = self . queue . qsize ( ) * bufferSize
2021-04-14 23:20:21 +00:00
print ( " Accumulated MB: " + str ( accumulatedData / 1000000 ) )
2021-04-20 14:32:56 +00:00
if queueLengthWait < 1 and accumulatedData > maxAccumulatedData :
2021-04-14 20:03:53 +00:00
print ( " Accumulated data limit reached. Closing. " )
self . connection . close ( )
self . queue = False
return
data = self . queue . get ( )
self . connection . sendall ( data )
except :
self . connection . close ( )
self . queue = False
raise
2021-04-13 23:09:34 +00:00
def listToCommand ( lst ) :
cmd = " "
for arg in lst :
arg = arg . replace ( " \\ " , " \\ \\ " )
arg = arg . replace ( " , " , " \\ , " )
#arg = arg.replace('"','\\"')
#if " " in arg: arg = '"' +arg+ '"'
cmd + = arg + " , "
return cmd [ : - 1 ]
def makePayload ( lst ) :
cmdText = listToCommand ( lst )
cmdBytes = cmdText . encode ( " utf-8 " )
while len ( cmdBytes ) < 1000 :
cmdBytes + = b " "
return cmdBytes
2021-04-13 20:06:16 +00:00
def main ( ) :
2021-04-14 15:23:36 +00:00
global serverAddr
serverAddr = sys . argv [ 1 ] . rsplit ( " : " , 1 )
serverAddr [ 1 ] = int ( serverAddr [ 1 ] )
serverAddr = tuple ( serverAddr )
2021-04-14 16:52:18 +00:00
connection . settimeout ( timeout )
2021-04-13 20:06:16 +00:00
connection . connect ( serverAddr )
2021-04-14 15:23:36 +00:00
connection . sendall ( makePayload ( sys . argv [ 2 : ] ) )
2021-04-13 23:09:34 +00:00
2021-04-14 15:23:36 +00:00
if sys . argv [ 2 ] == " watch " :
2021-04-14 20:03:53 +00:00
try :
stdoutThr = stdoutThread ( )
stdoutThr . start ( )
while True :
data = connection . recv ( bufferSize )
if data == b " " : return
stdoutThr . queue . put ( data )
except :
connection . close ( )
raise
2021-04-13 20:06:16 +00:00
2021-04-14 15:23:36 +00:00
if sys . argv [ 2 ] == " broadcast " :
2021-04-14 20:03:53 +00:00
try :
stdinThr = stdinThread ( connection )
stdinThr . start ( )
while True :
data = sys . stdin . buffer . read ( bufferSize )
stdinThr . queue . put ( data )
except :
connection . close ( )
raise
2021-04-13 18:33:16 +00:00
2021-04-13 20:06:16 +00:00
if __name__ == ' __main__ ' :
main ( )