python基于C/S模式實(shí)現(xiàn)聊天室功能
最簡(jiǎn)單的模式,C/S模式實(shí)現(xiàn)聊天室
從半雙工開(kāi)始,何謂半雙工?半雙工即是說(shuō)雙方可以互發(fā)消息,但一次只能一個(gè)用戶發(fā)送。
只要稍微會(huì)點(diǎn)socket編程的人都會(huì)覺(jué)得很簡(jiǎn)單,所以過(guò)過(guò)場(chǎng),直接上代碼。
服務(wù)器端代碼:
from socket import * from time import ctime HOST = '' PORT = 4568 BUFSIZE = 1024 ADDR = (HOST,PORT) tcpSerSocket = socket(AF_INET, SOCK_STREAM) tcpSerSocket.bind(ADDR) tcpSerSocket.listen(5) while True: print('waitint for connection...') tcpCliSocket, addr = tcpSerSocket.accept() print('connecting from: ', addr) while True: data = tcpCliSocket.recv(BUFSIZE) if not data: break print data msg = raw_input('>') tcpCliSocket.send('[%s] %s' % (ctime(), msg)) tcpCliSocket.close() tcpSerSocket.close()
客戶端代碼:
from socket import * HOST = 'localhost' PORT = 4568 BUFSIZE = 1024 ADDR = (HOST, PORT) tcpCliSocket = socket(AF_INET, SOCK_STREAM) tcpCliSocket.connect(ADDR) while True: data = raw_input('>>') if not data: break tcpCliSocket.send(data) data = tcpCliSocket.recv(BUFSIZE) if not data: break print data tcpCliSocket.close()
運(yùn)行結(jié)果我就不截圖了,如果還不會(huì)的就復(fù)制下來(lái)運(yùn)行一遍。
上面只是最簡(jiǎn)單的入門(mén),一點(diǎn)都不好使,問(wèn)題多著。
下面看怎么實(shí)現(xiàn)全雙工。全雙工就是雙方可任意給對(duì)方發(fā)送消息。
全雙工明顯要用到多線程。我們?cè)谥骶€程之外創(chuàng)建兩個(gè)子線程,一個(gè)負(fù)責(zé)接收消息,另一個(gè)負(fù)責(zé)接受用戶輸入并發(fā)送消息。
服務(wù)器端代碼:
#coding: utf-8 from socket import * from time import ctime import threading from sys import stdout HOST = '' PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) def Send(sck): while True: data = raw_input('>') sck.send(data) def Deal(sck, addr): while True: data = sck.recv(BUFSIZE) if data == "quit": sck.close() break str = '\nfrom' + addr[0] + ':' + data + '\n>' stdout.write(str) chatSerSock = socket(AF_INET, SOCK_STREAM) chatSerSock.bind(ADDR) chatSerSock.listen(5) threads = [] while True: print 'waiting for connection...' chatCliSock, addr = chatSerSock.accept() print "...connected from: ", addr t = threading.Thread(target=Deal, args=(chatCliSock, addr)) threads.append(t) t = threading.Thread(target=Send, args=(chatCliSock,)) threads.append(t) for i in range(len(threads)): threads[i].start() threads[0].join() chatCliSock.close() chatSerSock.close()
客戶端代碼:
#coding: utf8 from socket import * from time import ctime import threading from sys import stdout def Send(sck, test): while True: data = raw_input('>') sck.send(data) if data == "quit": break def Recieve(sck, test): while True: data = sck.recv(BUFSIZ) if data == "quit": sck.close() break str = "\nfrom server:" + data + "\n>" stdout.write(str) HOST = 'localhost' PORT= 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) threads = [] if __name__ == "__main__": chatCliSock = socket(AF_INET, SOCK_STREAM) chatCliSock.connect(ADDR) t = threading.Thread(target=Send, args = (chatCliSock, None)) threads.append(t) t = threading.Thread(target=Recieve, args = (chatCliSock, None)) threads.append(t) for i in range(len(threads)): threads[i].start() threads[0].join() chatCliSock.close()
運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python Socket網(wǎng)絡(luò)編程實(shí)現(xiàn)C/S模式和P2P
- python單線程文件傳輸?shù)膶?shí)例(C/S)
- Python實(shí)現(xiàn)基于C/S架構(gòu)的聊天室功能詳解
- Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能案例
- Python socket C/S結(jié)構(gòu)的聊天室應(yīng)用實(shí)現(xiàn)
- python3編寫(xiě)C/S網(wǎng)絡(luò)程序?qū)嵗坛?/a>
- 基于python實(shí)現(xiàn)簡(jiǎn)單C/S模式代碼實(shí)例
相關(guān)文章
python溫度轉(zhuǎn)換華氏溫度實(shí)現(xiàn)代碼
這篇文章主要介紹了python溫度轉(zhuǎn)換華氏溫度實(shí)現(xiàn)代碼內(nèi)容,有需要的朋友們可以測(cè)試下。2020-12-12pandas刪除行刪除列增加行增加列的實(shí)現(xiàn)
這篇文章主要介紹了pandas刪除行刪除列增加行增加列的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python腳本完成post接口測(cè)試的實(shí)例
今天小編就為大家分享一篇Python腳本完成post接口測(cè)試的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12pyqt 實(shí)現(xiàn)QlineEdit 輸入密碼顯示成圓點(diǎn)的方法
今天小編就為大家分享一篇pyqt 實(shí)現(xiàn)QlineEdit 輸入密碼顯示成圓點(diǎn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06python實(shí)現(xiàn)批量注冊(cè)網(wǎng)站用戶的示例
今天小編就為大家分享一篇python實(shí)現(xiàn)批量注冊(cè)網(wǎng)站用戶的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Python入門(mén)教程(二十一)Python的數(shù)組
這篇文章主要介紹了Python入門(mén)教程(二十一)Python的數(shù)組,數(shù)組是一種特殊變量,數(shù)組可以在單個(gè)名稱(chēng)下保存多個(gè)值,我們可以通過(guò)引用索引號(hào)來(lái)訪問(wèn)這些值,需要的朋友可以參考下2023-04-04