python实现聊天
转:https://www.cnblogs.com/nulige/p/6297829.html
server.py
import socket import select sk=socket.socket() sk.bind(("127.0.0.1",8801)) sk.listen(5) inputs=[sk,] while True: #监听sk和conn r,w,e=select.select(inputs,[],[],5) #conn发生变化,sk不变化就走else print(len(r)) #判断sk or conn 谁发生了变化 for obj in r: if obj==sk: conn,add=obj.accept() print(conn) inputs.append(conn) else: data_byte=obj.recv(1024) print(str(data_byte,‘utf8‘)) inp=input(‘回答%s号客户>>>‘%inputs.index(obj)) obj.sendall(bytes(inp,‘utf8‘)) print(‘>>‘,r)
client.py
import socket sk=socket.socket() sk.connect((‘127.0.0.1‘,8801)) while True: inp=input(">>>>") sk.sendall(bytes(inp,"utf8")) data=sk.recv(1024) print(str(data,‘utf8‘))
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16