Example #1
0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(CONNECTIONS_LIMIT)

try:
  print('Starting listening for socket connections…')
  while True:
    conn, addr = s.accept()
    SocketThread(radio, conn, addr).start()
except (KeyboardInterrupt, SystemExit):

  print('Exiting… Closing all connections…')
  radio.trigger('close connections')

  while True:
    conns_count = radio.request('opened connections count')
    if conns_count == 0: break
    sleep(0.1)

  conn_handler.__del__()
  del conn_handler
  btns.__del__()
  del btns
  radio.__del__()
  del radio

  s.shutdown(socket.SHUT_RDWR)

  print('Done')