forked from GanerCodes/videoEditBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketWrap.py
65 lines (51 loc) · 1.36 KB
/
socketWrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys, time, socket, threading, subprocess
from getSens import getSens
RESTART_DELAY_TIME = 1
HOST, PORT = getSens("log_ip")[0], 8080 #IP and port you want to send logs to
process, ID = sys.argv[1], sys.argv[2]
if len(sys.argv) > 3:
HAS_NAME = False
else:
HAS_NAME = True
if HAS_NAME: prefix = f"[{getSens('name')[0]}] "
else: prefix = ''
canCom = 0
s = socket.socket()
def tryConn():
global s
try:
s = socket.socket()
s.settimeout(1)
s.connect((HOST, PORT))
return True
except Exception as e:
return False
tryConn()
def sendMsg(msg):
global canCom, s
try:
s.sendall(msg)
except Exception as e:
if tryConn():
s.sendall(msg)
else:
canCom = 10
def threadMsg(msg):
t = threading.Thread(target = sendMsg, args = (msg, ))
t.start()
def send(msg):
global canCom
if canCom < 1: threadMsg(msg)
else: canCom -= 1
while 1:
proc = subprocess.Popen(process, stdout = subprocess.PIPE, universal_newlines = True, shell = True)
while proc.poll() is None:
out = proc.stdout.readline().strip()
print(out)
if out != "":
send((ID + '~' + prefix + out).encode('utf-8'))
s.close()
try: proc.kill()
except: pass
print("[SocketWrap] Process ended. Resarting.")
time.sleep(RESTART_DELAY_TIME)