python核心編程課後作業2-9,python核心課後2-9

來源:互聯網
上載者:User

python核心編程課後作業2-9,python核心課後2-9

網上找的代碼基礎上修複了一些問題。現在可以支援quit後繼續和其他人聊天。思路:A退出後,向B發送清除會話指令,B接收後再向伺服器返回清除會話指令,伺服器端清除B的會話(雖然繁瑣但是可以解決另一個問題:比如一個人輸入了和自己聊天的指令會導致chat_with值錯誤)。




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()


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。 //blog.csdn.net/qq_19342635/article/details/78915454

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.