python实现简单多人聊天室

时间:2022-09-15 13:24:12

本文实例为大家分享了python实现多人聊天室的具体代码,供大家参考,具体内容如下

刚开始学习python,写了一个聊天室练练手。

Server.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()

运行时先启动服务端。进入聊天室先起一个昵称。服务端会向客户端发送当前聊天室内聊天人的列表。一个客户端发出的消息会通过服务端发给其他客户端。

效果如下:

python实现简单多人聊天室

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/dk_zhe/article/details/37820965