import selectors
import socket
epoll = selectors.EpollSelector()
server = socket.socket()
server.bind(('', 8082))
server.listen(100)
def create_conneciton(server):
conn, addres = server.accept()
epoll.register(conn, selectors.EVENT_READ, read_data)
return conn
def read_data(conn):
data = conn.recv(1024)
if data:
print(data)
conn.send(data)
else:
epoll.unregister(conn)
epoll.register(server, selectors.EVENT_READ, create_conneciton)
while True:
events = epoll.select()
for key,mask in events:
sock = key.fileobj
callback = key.data
callback(sock)
import socket
client = socket.socket()
client.connect(('127.0.0.1', 8082))
while True:
data = input('输入数据:')
client.send(data.encode())
recv_data = client.recv(1024)
print(recv_data.decode())