server.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import socket, threading
  2. import time
  3. from threading import Thread
  4. import os
  5. f = open("DEAD.txt", "w")
  6. f.write("1")
  7. f.close()
  8. class ClientThread(threading.Thread):
  9. def __init__(self,clientAddress,clientsocket):
  10. threading.Thread.__init__(self)
  11. self.csocket = clientsocket
  12. print ("Новое подключение: ", clientAddress)
  13. def run(self):
  14. msg = ''
  15. while True:
  16. data = self.csocket.recv(4096)
  17. msg = data.decode()
  18. print(msg)
  19. self.csocket.send(bytes('Принято!!!','UTF-8'))
  20. f = open("DEAD.txt", "w")
  21. f.write("NO")
  22. f.close()
  23. if msg == '':
  24. print("Отключение")
  25. break
  26. LOCALHOST = "10.8.0.5"
  27. PORT = 7777
  28. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  30. server.bind((LOCALHOST, PORT))
  31. print("Сервер запущен!")
  32. def task():
  33. while True:
  34. time.sleep(15)
  35. print("Таймер окончен!")
  36. msgH = open("DEAD.txt", "r")
  37. msgR = msgH.read()
  38. qqq = str(msgR)
  39. print(qqq)
  40. if qqq == "NO":
  41. f = open("DEAD.txt", "w")
  42. f.write("YES")
  43. f.close()
  44. else:
  45. print("GAME OVER!")
  46. os.system("python3 /home/q/Programms/Dead/1/module.py")
  47. quit()
  48. def task2():
  49. while True:
  50. server.listen(1)
  51. clientsock, clientAddress = server.accept()
  52. newthread = ClientThread(clientAddress, clientsock)
  53. newthread.start()
  54. t1 = Thread(target=task)
  55. t2 = Thread(target=task2)
  56. t1.start()
  57. t2.start()
  58. t1.join()
  59. t2.join()