def new_chat_channel(conn, address):
""" New chat channel for a given connection """
participants.add(conn)
data = conn.recv(1024)
user = ''
while data:
print("Chat:", data.strip())
for p in participants:
try:
if p is not conn:
data = data.decode('utf-8')
user, msg = data.split(':')
if msg != '<handshake>':
data_s = '\n#[' + user + ']>>> says ' + msg
else:
data_s = '(User %s connected)\n' % user
p.send(bytearray(data_s, 'utf-8'))
except socket.error as e:
# ignore broken pipes, they just mean the participant
# closed its connection already
if e[0] != 32:
raise
data = conn.recv(1024)
participants.remove(conn)
print("Participant %s left chat." % user)
gevent_chat_server.py 文件源码
python
阅读 19
收藏 0
点赞 0
评论 0
评论列表
文章目录