Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室
本文實(shí)例為大家分享了Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室的具體代碼,供大家參考,具體內(nèi)容如下
服務(wù)端使用了select模塊,實(shí)現(xiàn)了對(duì)多個(gè)socket的監(jiān)控??蛻舳擞捎趕elect在Windows下只能對(duì)socket使用,所以使用了多線程來(lái)實(shí)現(xiàn)對(duì)客戶端輸入和socket連接的同時(shí)監(jiān)控。注意這里的socket設(shè)置為了非阻塞。這樣就實(shí)現(xiàn)了在一個(gè)線程中同時(shí)進(jìn)行socket的接收和發(fā)送。
服務(wù)器代碼:
# -*- coding: utf-8 -*- import socket,select connection_list = [] host = '' port = 10001 def board_cast(sock,message): for socket in connection_list: if socket != server_sock and socket != sock: try: socket.send(message) except: socket.close() print str(socket.getpeername())+' is offline' connection_list.remove(socket) server_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) server_sock.setblocking(0) server_sock.bind((host,port)) server_sock.listen(10) connection_list.append(server_sock) while 1: readable,writable,error = select.select(connection_list,[],[]) for sock in readable: if sock == server_sock: connection,connection_add = sock.accept() message = str(connection_add)+'enter room' board_cast(connection,message) print connection_add,'%s connect' connection_list.append(connection) else: try: date = sock.recv(1024) print date board_cast(sock,'('+str(sock.getpeername())+') :'+date) except: message2 = str(sock.getpeername())+ 'is offline' board_cast(sock,message2) print str(sock.getpeername())+ ' is offline' sock.close() connection_list.remove(sock) continue
客戶端代碼:
# -*- coding: utf-8 -*- import socket,threading,time flag = 0 date = '' lock = threading.Lock() host = 'localhost' port = 10001 client_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) client_sock.setblocking(0) class Mythread1(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): global flag, date while 1: date = raw_input() if len(date): lock.acquire() flag = 1 lock.release() class Mythread2(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): global flag global date while 1: try: buf = client_sock.recv(1024) if len(buf): print buf except: pass if flag: try: client_sock.send(date) except socket.error, e: print e lock.acquire() flag = 0 lock.release() try: client_sock.connect((host,port)) print"連接成功" except socket.error,e: print e t1 = Mythread1() t2 = Mythread2() t1.start() t2.start()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python socket多線程通訊實(shí)例分析(聊天室)
- Python聊天室?guī)Ы缑鎸?shí)現(xiàn)的示例代碼(tkinter,Mysql,Treading,socket)
- python socket 聊天室實(shí)例代碼詳解
- Python Socket編程之多線程聊天室
- python socket實(shí)現(xiàn)聊天室
- Python基于Socket實(shí)現(xiàn)簡(jiǎn)單聊天室
- 基于Python socket實(shí)現(xiàn)簡(jiǎn)易網(wǎng)絡(luò)聊天室
- python使用socket制作聊天室詳細(xì)源碼(可以直接運(yùn)行)
相關(guān)文章
python統(tǒng)計(jì)字符串中字母出現(xiàn)次數(shù)代碼實(shí)例
這篇文章主要介紹了python統(tǒng)計(jì)字符串中字母出現(xiàn)次數(shù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python實(shí)現(xiàn)的北京積分落戶數(shù)據(jù)分析示例
這篇文章主要介紹了Python實(shí)現(xiàn)的北京積分落戶數(shù)據(jù)分析,結(jié)合實(shí)例形式分析了Python針對(duì)北京積分落戶數(shù)據(jù)的分析、運(yùn)算、展示等相關(guān)操作技巧,需要的朋友可以參考下2020-03-03Python實(shí)現(xiàn)修改文件內(nèi)容的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)修改文件內(nèi)容的方法,結(jié)合實(shí)例形式分析了Python文件讀寫(xiě)、字符串替換及shell方法調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例,排序算法、查找算法和圖遍歷算法是計(jì)算機(jī)科學(xué)中常見(jiàn)且重要的算法。它們?cè)跀?shù)據(jù)處理、搜索和圖結(jié)構(gòu)等領(lǐng)域發(fā)揮著關(guān)鍵作用,需要的朋友可以參考下2023-08-08提升Python項(xiàng)目整潔度使用import?linter實(shí)例探究
在復(fù)雜的Python項(xiàng)目中,良好的代碼組織結(jié)構(gòu)是維護(hù)性和可讀性的關(guān)鍵,本文將深入研究?import-linter?工具,它是一個(gè)強(qiáng)大的靜態(tài)分析工具,旨在優(yōu)化項(xiàng)目的模塊導(dǎo)入,提高代碼質(zhì)量和可維護(hù)性2024-01-01Python編程functools模塊創(chuàng)建修改的高階函數(shù)解析
本篇文章主要為大家介紹functools模塊中用于創(chuàng)建、修改函數(shù)的高階函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-09-09