如何使用python socketserver.TCPServer建立多客户端聊天室

时间:2019-06-01 02:05:22

标签: python multithreading socketserver

我必须使用Python的serversocket模块提供的TCPServer来建立多客户端聊天室。如何使this成为多客户端服务器并将消息发送给所有其他客户端?

我尝试修改示例中的现有代码

... python

import socket
import threading
import socketserver

clientList = []
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

    def handle(self):
        clientList.append(self.client_address)
        print(clientList)
        print("Client List Length : ",len(clientList))

        while True:
            data = str(self.request.recv(1024), 'ascii')
            if(data.upper() == "EXIT"):
                break
            cur_thread = threading.current_thread()
            response = bytes(data, 'utf_8')
            #self.request.sendall(response)

            for cl in range(1,len(clientList)):
                print("sending to : ",clientList[cl])
                self.request.sendto(response,clientList[cl])

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass

def passtime():
    pass
if __name__ == "__main__":

    HOST, PORT = "localhost", 50007


server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
ip, port = server.server_address



# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)

# Exit the server thread when the main thread terminates
server_thread.daemon = True
server_thread.start()
while server_thread:
   passtime() 
print("Server loop running in thread:", server_thread.name)

...

我有一个问题:     self.request.sendto(request,clientList[cl])

仅将请求发送回发送客户端,而不发送回客户端列​​表中的目标客户端。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,就是这样:

import socket
import threading
import socketserver
import sys
import select

clientList = []
inbox = []


class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
    clients = []    
    msgSend = 0
    def setup(self):
        clientList.append(self.client_address)
        self.clients = list(dict.fromkeys(clientList))
        print(self.clients)
        print("Client List Length : ",len(self.clients))

    def handle(self):
        while True:
            r,w,e = select.select([self.request],[],[],0.01)
            for rs in r:
                if rs == self.request:
                    data = str(self.request.recv(1024),"ascii")
                    if data:
                        inbox.append(data)
                else:
                    if self.msgSend < len(inbox):
                        for elem in range(self.msgSend, len(inbox)):
                            print("server send :",inbox[elem])
                            self.request.sendall(bytes(inbox[elem],'utf-8'))
                        self.msgSend += 1

            if self.msgSend < len(inbox):
                for elem in range(self.msgSend, len(inbox)):
                    print("server send :",inbox[elem])
                    self.request.sendall(bytes(inbox[elem],'utf-8'))
                self.msgSend += 1

    def finish(self):
        for l in range(len(clientList)):
            if self.client_address == clientList[l]:
                clientList.remove(l)

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass

def passtime():
    pass



if __name__ == "__main__":

    HOST, PORT = "localhost", 50007
    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    server_thread = threading.Thread(target=server.serve_forever)


    server_thread.daemon = True
    server_thread.start()
    while server_thread:
       passtime() 


    sys.exit()

我使用here中的收件箱方法,使用select.select可以检查是否有要读取的输入。