国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python socket實現(xiàn)簡單聊天室

2020-01-04 15:29:44
字體:
供稿:網(wǎng)友

本文實例為大家分享了Python socket實現(xiàn)簡單聊天室的具體代碼,供大家參考,具體內(nèi)容如下

服務(wù)端使用了select模塊,實現(xiàn)了對多個socket的監(jiān)控。客戶端由于select在Windows下只能對socket使用,所以使用了多線程來實現(xiàn)對客戶端輸入和socket連接的同時監(jiān)控。注意這里的socket設(shè)置為了非阻塞。這樣就實現(xiàn)了在一個線程中同時進行socket的接收和發(fā)送。

服務(wù)器代碼:

# -*- coding: utf-8 -*-import socket,selectconnection_list = []host = ''port = 10001def 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,timeflag = 0date = ''lock = threading.Lock()host = 'localhost'port = 10001client_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 et1 = Mythread1()t2 = Mythread2()t1.start()t2.start()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宿迁市| 积石山| 玉门市| 华容县| 宽城| 远安县| 闵行区| 吴堡县| 武鸣县| 梅河口市| 八宿县| 林周县| 久治县| 广水市| 宜君县| 汪清县| 互助| 即墨市| 龙井市| 株洲县| 广饶县| 福贡县| 深州市| 工布江达县| 黄山市| 宾阳县| 济南市| 禹城市| 乌拉特中旗| 安阳市| 遵义市| 宣威市| 乡城县| 龙山县| 清流县| 中西区| 石门县| 宁乡县| 新丰县| 沐川县| 皋兰县|