import socket
import thread
import time

HOST = 
PORT = 4004

def accept(conn):
    
    def threaded():
        while True:
            conn.send()
            try:
                name = conn.recv(1024).strip()
            except socket.error:
                continue
            if name in users:
                conn.send()
            elif name:
                conn.setblocking(False)
                users[name] = conn
                broadcast(name, % name)
                break
    thread.start_new_thread(threaded, ())

def broadcast(name, message):
    
    print message
    for to_name, conn in users.items():
        if to_name != name:
            try:
                conn.send(message + )
            except socket.error:
                pass


server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.setblocking(False)
server.bind((HOST, PORT))
server.listen(1)
print % (% server.getsockname())


users = {}
while True:
    try:
        
        while True:
            try:
                conn, addr = server.accept()
            except socket.error:
                break
            accept(conn)
        
        for name, conn in users.items():
            try:
                message = conn.recv(1024)
            except socket.error:
                continue
            if not message:
                
                del users[name]
                broadcast(name, % name)
            else:
                broadcast(name, % (name, message.strip()))
        time.sleep(.1)
    except (SystemExit, KeyboardInterrupt):
        break