利用io多路复用实现并发聊天
#server端
#_author:来童星
#date:2019/12/29
#利用io多路复用实现并发聊天
import socket
import select
sk=socket.socket()
address=('127.0.0.1',8800)
sk.bind(address)
sk.listen(3)
inp=[sk,]
while True:
inputs,outputs,error=select.select(inp,[],[],)# [inp]---- TypeError: argument must be an int, or have a fileno() method.
for obj in inputs:
if obj==sk:
coon,addr=sk.accept()
print(coon)
inp.append(coon)
else:
data=obj.recv(1024)
print(data.decode('utf8'))
Inp=input('>>>')
obj.sendall(bytes(Inp,'utf8'))
================================================================================================================================
client端:
#_author:来童星
#date:2019/12/29
import socket
import select
sk=socket.socket()
sk.connect(('127.0.0.1',8800))
while True:
inp=input('>>>')
if inp=='bye':
break
sk.sendall(bytes(inp,'utf8'))
data=sk.recv(1024)
print(data.decode('utf8'))
=================================================================================================================================