wsread.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. def conn(target):
  17. global ws
  18. if ws is None:
  19. ws = websocket.WebSocket()
  20. try:
  21. ws.connect(target)
  22. except Exception as e:
  23. time.sleep(2)
  24. conn(target) #keep trying you basterd
  25. def close():
  26. if ws is None:
  27. return
  28. ws.close()
  29. def sighandler(signal,frame):
  30. print("Exiting!")
  31. close()
  32. print("Bye!")
  33. sys.exit(0)
  34. def stop(key):
  35. if key not in p_list:
  36. print("key %s doesn't exist! not killing!" % (key))
  37. return
  38. os.killpg(os.getpgid(p_list[key].pid), signal.SIGTERM)
  39. try:
  40. log_list[key].close()
  41. except Exception as e:
  42. print("ex file: %s" % e)
  43. if key in log_list:
  44. del log_list[key]
  45. del p_list[key]
  46. def launch(key,options=None):
  47. if key in p_list:
  48. print("key %s exist! not running!" % (key))
  49. return
  50. tolaunch = ptable[j["key"]]
  51. if options is not None:
  52. tolaunch = tolaunch + options
  53. log_list[key]=open("/tmp/%s" % key, "w")
  54. proc=subprocess.Popen(tolaunch, preexec_fn=os.setsid, stdout=log_list[key])
  55. #Le doy su propio grupo al subproceso
  56. p_list[key]=proc
  57. return proc.pid
  58. def sendmsg(msg):
  59. s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
  60. ret = None
  61. try:
  62. s.connect(('localhost',9999))
  63. tosend = msg + "\r\n"
  64. s.sendall(tosend.encode("ascii"))
  65. ret=s.recv(4096)
  66. s.close()
  67. except Exception as e:
  68. print(e)
  69. return None
  70. print(ret)
  71. return ret
  72. ip="ws://192.168.1.208:9001"
  73. conn(ip)
  74. signal.signal(signal.SIGINT, sighandler)
  75. signal.signal(signal.SIGCHLD, signal.SIG_IGN) #To kill zombies
  76. while True:
  77. try:
  78. data = ws.recv()
  79. except Exception as e:
  80. print(e)
  81. conn(ip)
  82. continue
  83. try:
  84. j = json.loads(data)
  85. except Exception as e:
  86. print(e)
  87. print("Invalid json?")
  88. continue
  89. if ("type" not in j or "command" not in j or
  90. "key" not in j or j["type"] != "snowmix"):
  91. continue
  92. if j["command"] == "launch":
  93. launch(j["key"])
  94. if j["command"] == "kill":
  95. stop(j["key"])
  96. if j["command"] == "netcat":
  97. sendmsg(j["key"])