wsread.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. import signal
  5. import socket
  6. import subprocess
  7. import sys
  8. import websocket
  9. ws = websocket.WebSocket()
  10. p_list = {}
  11. ptable = {}
  12. ptable["audio"] = [ "/home/nginx/scripts-snowmix/working-udp-audio.sh"]
  13. ptable["video"] = [ "/home/nginx/scripts-snowmix/working-udp-video.sh", "1", "0" ]
  14. ptable["rtmp"] = [ "/home/nginx/scripts-snowmix/stream.sh"]
  15. log_list = {}
  16. DEBUG = False
  17. def log(s):
  18. if DEBUG == False:
  19. return
  20. print(s)
  21. def conn(target):
  22. global ws
  23. if ws is None:
  24. ws = websocket.WebSocket()
  25. try:
  26. ws.connect(target)
  27. except Exception as e:
  28. time.sleep(2)
  29. conn(target) #keep trying you basterd
  30. def close():
  31. if ws is None:
  32. return
  33. ws.close()
  34. def sighandler(signal,frame):
  35. log("\rExiting!")
  36. close()
  37. log("Bye!")
  38. sys.exit(0)
  39. def stop(key):
  40. if key not in p_list:
  41. log("key %s doesn't exist! not killing!" % (key))
  42. return
  43. try:
  44. os.killpg(os.getpgid(p_list[key].pid), signal.SIGTERM)
  45. except Exception as e:
  46. log("This process (%d) can't be killed! Why?" % p_list[key].pid)
  47. log(e)
  48. try:
  49. log_list[key].close()
  50. except Exception as e:
  51. log("ex file: %s" % e)
  52. if key in log_list:
  53. del log_list[key]
  54. del p_list[key]
  55. def launch(key,options=None):
  56. if key in p_list:
  57. log("key %s exist! not running!" % (key))
  58. return
  59. if key not in ptable:
  60. log("This process (%s) is not on my table!" % key)
  61. return
  62. tolaunch = ptable[key]
  63. if options is not None:
  64. tolaunch = tolaunch + options
  65. log_list[key]=open("/tmp/%s" % key, "w")
  66. proc=subprocess.Popen(tolaunch, preexec_fn=os.setsid, stdout=log_list[key])
  67. #Le doy su propio grupo al subproceso
  68. p_list[key]=proc
  69. return proc.pid
  70. def sendmsg(msg):
  71. s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
  72. ret = None
  73. try:
  74. s.connect(('localhost',9999))
  75. tosend = msg + "\r\n"
  76. s.sendall(tosend.encode("ascii"))
  77. ret=s.recv(4096)
  78. s.close()
  79. except Exception as e:
  80. log(e)
  81. return None
  82. log("[nc output] %s " % ret)
  83. return ret
  84. ip="ws://192.168.1.208:9001"
  85. conn(ip)
  86. signal.signal(signal.SIGINT, sighandler)
  87. signal.signal(signal.SIGCHLD, signal.SIG_IGN) #To kill zombies
  88. while True:
  89. try:
  90. data = ws.recv()
  91. except Exception as e:
  92. log(e)
  93. conn(ip)
  94. continue
  95. try:
  96. j = json.loads(data)
  97. except Exception as e:
  98. log(e)
  99. log("Invalid json?")
  100. continue
  101. if ("type" not in j or "command" not in j or
  102. "key" not in j or j["type"] != "snowmix"):
  103. log("Dropping: %s" % j)
  104. continue
  105. log(j)
  106. if j["command"] == "launch":
  107. launch(j["key"])
  108. if j["command"] == "kill":
  109. stop(j["key"])
  110. if j["command"] == "netcat":
  111. sendmsg(j["key"])