class ChatServer():
    def __init__(self, port):
        self.clients = {}
        self.port = port
        self.server_socket = ReadWriteSocket()
        self.keep_running = False
    
    def start(self):
        self.server_socket.bind(('', self.port))
        self.server_socket.listen(5)
        print "Listening for connections..."
        self.keep_running = True
        sockets = [self.server_socket]
        nicknames = {}
        while self.keep_running:
            readable, writable, error = select.select(sockets, [], [])
            if len(readable) > 0:
                for sock in readable:
                    if sock is self.server_socket:
                        print "New client accepted."
                        (client_socket, address) = sock.accept()
                        sockets.append(client_socket)
                    else:
                        msg = sock.readnext()
                        if not sock in nicknames.keys():
                            if msg in nicknames.values():
                                sock.write("ERROR: Nickname already in use.")
                            else:
                                nicknames[sock] = msg
                                sock.write("SERVER: Welcome to chat!")
                        elif msg == "":
                            sock.close()
                            sockets.remove(sock)
                            print "Client disconnected."
                        else:
                            for client_socket in sockets:
                                if not client_socket is self.server_socket:
                                    client_socket.write("[%s]> %s" % (nicknames[sock], msg))
                            print msg
        for socket in sockets:
            socket.close()    
    def exit(self):
        self.keep_running = False
Beispiel #2
0
    def connect(self):
        self.sock = ReadWriteSocket()
        self.sock.connect((self.server, self.port))
        self.keep_running = True
        initialized = False
        prompt = "Enter a nickname: "
        nickname = None
        print prompt

        while self.keep_running and nickname is None:
            nickname = sys.stdin.readline().strip()
            self.sock.write(nickname)
            reply = self.sock.readnext()
            print reply
            if not reply.startswith("ERROR"):
                break

        threading.Thread(target=self.server_handler).start()
        while self.keep_running:
            sys.stdout.write(">")
            msg = sys.stdin.readline().strip()
            if msg != "":
                self.sock.write(msg)
 def connect(self):
     self.sock = ReadWriteSocket()
     self.sock.connect((self.server, self.port))
     self.keep_running = True
     initialized = False
     prompt = "Enter a nickname: "
     nickname = None
     print prompt
     
     while self.keep_running and nickname is None:
         nickname = sys.stdin.readline().strip()
         self.sock.write(nickname)
         reply = self.sock.readnext()
         print reply
         if not reply.startswith("ERROR"):
             break
     
     threading.Thread(target=self.server_handler).start()
     while self.keep_running:
         sys.stdout.write(">")
         msg = sys.stdin.readline().strip()
         if msg != "":
             self.sock.write(msg)
Beispiel #4
0
class ChatClient():
    def __init__(self, server, port):
        self.server = server
        self.port = port
        self.keep_running = False
        self.sock = None

    def connect(self):
        self.sock = ReadWriteSocket()
        self.sock.connect((self.server, self.port))
        self.keep_running = True
        initialized = False
        prompt = "Enter a nickname: "
        nickname = None
        print prompt

        while self.keep_running and nickname is None:
            nickname = sys.stdin.readline().strip()
            self.sock.write(nickname)
            reply = self.sock.readnext()
            print reply
            if not reply.startswith("ERROR"):
                break

        threading.Thread(target=self.server_handler).start()
        while self.keep_running:
            sys.stdout.write(">")
            msg = sys.stdin.readline().strip()
            if msg != "":
                self.sock.write(msg)

    def server_handler(self):
        while self.keep_running:
            readables, writables, error = select.select([self.sock], [], [], 1)
            for s in readables:
                print s.readnext()
class ChatClient():
    def __init__(self, server, port):
        self.server = server
        self.port = port
        self.keep_running = False
        self.sock = None
        
    def connect(self):
        self.sock = ReadWriteSocket()
        self.sock.connect((self.server, self.port))
        self.keep_running = True
        initialized = False
        prompt = "Enter a nickname: "
        nickname = None
        print prompt
        
        while self.keep_running and nickname is None:
            nickname = sys.stdin.readline().strip()
            self.sock.write(nickname)
            reply = self.sock.readnext()
            print reply
            if not reply.startswith("ERROR"):
                break
        
        threading.Thread(target=self.server_handler).start()
        while self.keep_running:
            sys.stdout.write(">")
            msg = sys.stdin.readline().strip()
            if msg != "":
                self.sock.write(msg)

    def server_handler(self):
        while self.keep_running:
            readables, writables, error = select.select([self.sock], [], [], 1)
            for s in readables:
                print s.readnext()
 def __init__(self, port):
     self.clients = {}
     self.port = port
     self.server_socket = ReadWriteSocket()
     self.keep_running = False