def accept_cons(): print "Started accepting" try: conns.sock.bind((conns.HOST, conns.PORT)) except: print "Unable to bind socket" conns.FLAG = True conns.sock.setblocking(0) while True: #change this so that the program can exit cleanly if conns.FLAG: break sleep(0.05) try: conns.sock.listen(1) conn, addr = conns.sock.accept() except: continue print "Connected to ", addr conns.send(conns.SERVER_STR, conn, "Welcome") conn.setblocking(0) #set the socket to non blocking #preventing concurrent access lock.acquire() conns.clients[conn] = addr conns.nicks[conn] = addr[0] messages.broadcast_all(conns.SERVER_STR, conns.nicks.get(conn) + " connected") lock.release() print "done listening for conns"
def parse_msg(msg, con): strings = msg.split(" ") cmd = strings[0] if cmd in commands: if cmd == "/dc": server_dc(" ".join(strings[commands.get(cmd):]), con) elif cmd == "/stop": stop(strings[1]) elif cmd == "/join": join(strings[1]) elif cmd == "/leave": leave(strings[1]) elif cmd == "/setnick": set_nick(strings[1], con) elif cmd == "/to": pass else: #msg does not contain a command for person in conns.clients.keys(): if person == con: continue conns.send(conns.nicks.get(con), person, msg)
def broadcast_all(sender, msg): for person in conns.clients.keys(): conns.send(sender, person, msg)