python核心编程课后作业2-9
2017-12-27 19:11
260 查看
网上找的代码基础上修复了一些问题。现在可以支持quit后继续和其他人聊天。思路:A退出后,向B发送清除会话指令,B接收后再向服务器返回清除会话指令,服务器端清除B的会话(虽然繁琐但是可以解决另一个问题:比如一个人输入了和自己聊天的指令会导致chat_with值错误)。
server_test.py
client_test.py
server_test.py
import threading import re from socket import * HOST='127.0.0.1' PORT=21568 BUFSIZ=1024 ADDR=(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-8 from socket import * from time import ctime import threading HOST='localhost' PORT=21568 BUFSIZ=1024 ADDR=(HOST,PORT) def sendMsg(sock): while True: data = raw_input('>') sock.send(data) if data == 'quit': break def 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()
相关文章推荐
- python核心编程课后作业2-10
- JavaScript第五章课后作业
- Python第一天课后作业
- Ng机器学习课程第五周神经网络反向传播算法课后作业初学者的理解
- 吴恩达 深度学习 1-3 课后作业 Planar data classification with one hidden layer
- 吴恩达深度学习1-4课后作业1 Building your Deep Neural Network: Step by Step
- 多线程课后作业
- Linux运维笔记-课后作业-简单登陆与使用
- 课后作业(五)String对象的常用属性和方法
- 课后作业之成绩类
- C++课后习题作业
- 171129课后作业
- 第十一章 类的无参方法 课后作业
- 第十四章课后作业
- C语言课后作业1
- 12.14课堂笔记、课后作业、学习心得
- 课后作业--1:《软件需求与分析》博文读后感
- 機器學習基石(Machine Learning Foundations) 机器学习基石 作业三 课后习题解答
- HTML 第八章课后作业5
- 面向对象程序设计 (C++语言描述)第一章课后习题作业