python實(shí)現(xiàn)簡(jiǎn)單多人聊天室
本文實(shí)例為大家分享了python實(shí)現(xiàn)多人聊天室的具體代碼,供大家參考,具體內(nèi)容如下
剛開(kāi)始學(xué)習(xí)python,寫(xiě)了一個(gè)聊天室練練手。
Server.py
import socket,select,thread;
host=socket.gethostname()
port=5963
addr=(host,port)
inputs=[]
fd_name={}
def who_in_room(w):
name_list=[]
for k in w:
name_list.append(w[k])
return name_list
def conn():
print 'runing'
ss=socket.socket()
ss.bind(addr)
ss.listen(5)
return ss
def new_coming(ss):
client,add=ss.accept()
print 'welcome %s %s' % (client,add)
wel='''welcome into the talking room .
please decide your name.....'''
try:
client.send(wel)
name=client.recv(1024)
inputs.append(client)
fd_name[client]=name
nameList="Some people in talking room, these are %s" % (who_in_room(fd_name))
client.send(nameList)
except Exception,e:
print e
def server_run():
ss=conn()
inputs.append(ss)
while True:
r,w,e=select.select(inputs,[],[])
for temp in r:
if temp is ss:
new_coming(ss)
else:
disconnect=False
try:
data= temp.recv(1024)
data=fd_name[temp]+' say : '+data
except socket.error:
data=fd_name[temp]+' leave the room'
disconnect=True
if disconnect:
inputs.remove(temp)
print data
for other in inputs:
if other!=ss and other!=temp:
try:
other.send(data)
except Exception,e:
print e
del fd_name[temp]
else:
print data
for other in inputs:
if other!=ss and other!=temp:
try:
other.send(data)
except Exception,e:
print e
if __name__=='__main__':
server_run()
client.py
import socket,select,threading,sys;
host=socket.gethostname()
addr=(host,5963)
def conn():
s=socket.socket()
s.connect(addr)
return s
def lis(s):
my=[s]
while True:
r,w,e=select.select(my,[],[])
if s in r:
try:
print s.recv(1024)
except socket.error:
print 'socket is error'
exit()
def talk(s):
while True:
try:
info=raw_input()
except Exception,e:
print 'can\'t input'
exit()
try:
s.send(info)
except Exception,e:
print e
exit()
def main():
ss=conn()
t=threading.Thread(target=lis,args=(ss,))
t.start()
t1=threading.Thread(target=talk,args=(ss,))
t1.start()
if __name__=='__main__':
main()
運(yùn)行時(shí)先啟動(dòng)服務(wù)端。進(jìn)入聊天室先起一個(gè)昵稱(chēng)。服務(wù)端會(huì)向客戶(hù)端發(fā)送當(dāng)前聊天室內(nèi)聊天人的列表。一個(gè)客戶(hù)端發(fā)出的消息會(huì)通過(guò)服務(wù)端發(fā)給其他客戶(hù)端。
效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)OCR識(shí)別之pytesseract案例詳解
這篇文章主要介紹了Python實(shí)現(xiàn)OCR識(shí)別之pytesseract案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Python實(shí)現(xiàn)多功能音樂(lè)播放器詳解
這篇文章主要介紹了如何通過(guò)Python制作一個(gè)簡(jiǎn)易的音樂(lè)播放器,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定價(jià)值,需要的可以參考一下2022-02-02
Python?ConfigParser庫(kù)輕松讀寫(xiě)INI文件實(shí)例探究
這篇文章主要為大家介紹了Python?ConfigParser庫(kù)輕松讀寫(xiě)INI文件實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
深入理解Python密碼學(xué)之使用PyCrypto庫(kù)進(jìn)行加密和解密
Python中的Pycrypto庫(kù)是一個(gè)廣泛使用的密碼學(xué)工具包,它為開(kāi)發(fā)者提供了多種加密算法,包括著名的RSA加密算法,這篇文章主要給大家介紹了關(guān)于Python密碼學(xué)之使用PyCrypto庫(kù)進(jìn)行加密和解密的相關(guān)資料,需要的朋友可以參考下2024-07-07
python爬蟲(chóng)將js轉(zhuǎn)化成json實(shí)現(xiàn)示例
這篇文章主要為大家介紹了python爬蟲(chóng)將js轉(zhuǎn)化成json實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
pytorch通過(guò)訓(xùn)練結(jié)果的復(fù)現(xiàn)設(shè)置隨機(jī)種子
這篇文章主要介紹了pytorch通過(guò)訓(xùn)練結(jié)果的復(fù)現(xiàn)設(shè)置隨機(jī)種子的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

