自学python一段时间,一直想弄个有意思的东西,所以就拿socket做一个聊天室,可以一对多,一对一全双工聊天。后续可能完善代码在鼓弄一个带gui界面的,比较有逼格。
服务端:
使用socketserver模块,多线程异步处理客户端消息,接受客户消息并转发 既服务端为一个中转站。
加入了 登陆 注册 多人聊天 一对一功能
客户端:
主线程连接服务端,两个子线程分别负责读写
server:
SocketServer time ctime threadingtraceback Queue db DB lock=threading.Lock() local_school = threading.local() Handler(): queue = [] db = DB() user_name = {} (sock): .sock = sock .input = [.sock] (): data = .sock.recv().strip() data (data): .sock.sendall(data) (): .send() .sock.close() .queue.remove(.sock) .user_name[local_school.user] (): .sock.close() .queue.remove(.sock) .user_name[local_school.user] (userdata): sock .queue: sock.sendall(% (ctime()userdata)) (user_sockuserdata): .user_name[user_sock].sendall(% (ctime()userdata)) (userdata): time_data = ctime() sock [x x .queue x != .sock]: sock.sendall(% (time_datauserdata)) (): funcdict = { : .login: .registered} : .sock.send() data = .recv() data == : .stop() data funcdict: funcdict[data]() : .handler() : .exit() (): .send() data = .recv() data == : .send() data = .recv() data == : .stop() data == : .handler() : .login() user_data = data.split() (user_data) == : user = user_data[] passwd = user_data[] user_data = .db.get_data() {} user user_data user_data[user] == passwd: local_school.user=user .send() .queue.append(.sock) .broadcast(% user) .user_name[user]=.sock .send() data=.recv() data==: .Ltian() data==: .one_to_one() : .send() .handler() : .send() .login() : .send() .login() (): .send() data = .recv() data == : .send() data = .recv() data == : .stop() .send() : .handler() user_data = data.split() (user_data) == : user = user_data[] passwd = user_data[] db_data = .db.get_data() {} user db_data: .send() .registered() : db_data[user] = passwd local_school.user=user lock.acquire() : .db.put_data(db_data) : lock.release() .broadcast(% user) .queue.append(.sock) .user_name[user] = .sock .Ltian() .queue : .send() .registered() (): .queue .send() : data = .recv() data==: % .queue .stop()% .queue .yiduiduo(local_school.userdata) (): .send() user_data=.recv()[:] user_data==local_school.user: .one_to_one() user_data .db.get_data(): .user_name.get(user_data) .user_name[user_data] .queue: .send() : data=.recv() data==: .send() data=.recv() data==: .one(user_datalocal_school.user) .stop() data==: .Ltian() data==.user_name.get(user_data):.one(user_datalocal_school.userdata) : .send() .one_to_one() : .send() .one_to_one() MyServer(SocketServer.BaseRequestHandler): (): .client_address .mysock = Handler(.request) .mysock.queue .mysock.handler() __name__ == : host = port = addr = (hostport) server = SocketServer.ThreadingTCPServer(addrMyServer) server.request_queue_size=server.serve_forever()
client:
socket * threading threads=[] Client_Handler(): (ipadr=port=): .sock = socket(AF_INETSOCK_STREAM) .sock.connect((ipadrport)) .input=[.sock] .input (data): .sock.sendall(data) (): data = .sock.recv().strip() data data (): : : data=() data==: .send() .sock.close() .send(data) : (): : : .recv() : a1=Client_Handler() chat = threading.Thread(=a1.write) threads.append(chat) chat = threading.Thread(=a1.read) threads.append(chat) threads i ((threads)): threads[i].start()
时间: 2024-11-07 00:47:43