单进程服务器-非堵塞模式
import socket
client_list = []
def main():
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
address = ('', 8000)
server_sock.bind(address)
server_sock.listen(128)
server_sock.setblocking(False)
while True:
try:
client_info = server_sock.accept()
except BlockingIOError:
pass
else:
print("一个新的客户端到来%s:" % str(client_info))
client_info[0].setblocking(False)
client_list.append(client_info)
need_del_client_info_list = []
for client_socket, client_addr in client_list:
try:
recv_data = client_socket.recv(1024)
if len(recv_data) > 0:
print('recv[%s]:%s' % (str(client_addr), recv_data.decode()))
else:
print('[%s]客户端已经关闭' % str(client_addr))
client_socket.close()
need_del_client_info_list.append((client_socket, client_addr))
except BlockingIOError:
pass
for client in need_del_client_info_list:
client_list.remove(client)
if __name__ == '__main__':
main()