Python core programming homework 2-9, python core homework 2-9
Some problems are fixed based on the code found on the Internet. Now you can continue to chat with other people after quit is supported. Idea: After A exits, it sends the clear session command to B. After B receives the command, it returns the clear session command to the server. The server clears the session of B (although tedious, it can solve another problem: for example, if a user enters a command to chat with himself, the chat_with value will be incorrect ).
Server_test.py
import threadingimport refrom socket import *HOST='127.0.0.1'PORT=21568BUFSIZ=1024ADDR=(HOST,PORT)tcpSerSock = socket(AF_INET,SOCK_STREAM)tcpSerSock.bind(ADDR)tcpSerSock.listen(5)def sendMsg(socket, username): while True: data = socket.recv(BUFSIZ) if data == 'quit': if chatwith.has_key(username): chatwith[username].send((username + ':' + data)) chatwith[username].send('cmdcancel') del chatwith[username] del account[username] socket.send(data) socket.close() print '%s logout' % username break elif data == 'cmdcancel': del chatwith[username] elif re.match(r'to:.+',data) is not None: chat_with = data[3:] if chat_with == username: socket.send('Can`t chat with yourself') elif chatwith.has_key(chat_with): socket.send('the user %s is bussy' % chat_with) elif account.has_key(chat_with): chatwith[username]=account[chat_with] chatwith[chat_with]=socket else: socket.send('the user %s is not exist' % chat_with) else: if chatwith.has_key(username): chatwith[username].send(data) else: socket.send('you want to chat with-> to:username ')account = {}chatwith = {}while True: print 'waiting for connection...' tcpCliSock,addr=tcpSerSock.accept() print '...connected from:', addr username = tcpCliSock.recv(BUFSIZ) print 'username:%s' % username if account.has_key(username): tcpCliSock.send("Reuse") tcpCliSock.close() else: tcpCliSock.send("Welcome!") account[username]=tcpCliSock recev_msg = threading.Thread(target=sendMsg, args=(tcpCliSock,username)) recev_msg.start()tcpSerSock.close()
Client_test.py
#coding=utf-8from socket import *from time import ctimeimport threadingHOST='localhost'PORT=21568BUFSIZ=1024ADDR=(HOST,PORT)def sendMsg(sock): while True: data = raw_input('>') sock.send(data) if data == 'quit': breakdef recvMsg(sock): while True: data = tcpCliSock.recv(BUFSIZ) if data == 'cmdcancel': sock.send('cmdcancel') continue elif data == 'quit': sock.close() print data break print '[%s] %s' % (ctime(),data)tcpCliSock=socket(AF_INET,SOCK_STREAM)tcpCliSock.connect(ADDR)username = raw_input('please input your name:')tcpCliSock.send(username)data = tcpCliSock.recv(BUFSIZ)if data == 'Reuse': print 'The username has been used!'else: send_msg = threading.Thread(target=sendMsg, args=(tcpCliSock,)) recv_msg = threading.Thread(target=recvMsg, args=(tcpCliSock,)) send_msg.start() recv_msg.start() send_msg.join()tcpCliSock.close()
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. // Blog.csdn.net/qq_19342635/article/details/78915454