标签:pre 列表 connect imp lis 数据 art false 客户端
服务端 from socket import * import time import select server = socket(AF_INET,SOCK_STREAM) server.bind((‘127.0.0.1‘,8081)) server.listen(3) server.setblocking(False) #accept变为非阻塞状态, print(‘server startting...‘) reads_l = [server,] #每次来一个客户端,添加到列表 while True: r_l,_,_ = select.select(reads_l,[],[]) for obj in r_l: #接收到的对象 if obj == server: #server.accept conn,addr = server.accept conn,addr = obj.accept() #每个对象都有conn,addr reads_l.append(conn) else: # conn.recv 接收到的数据 data = obj.recv(1024) #obj = conn obj.send(data.upper()) 客户端 from socket import * client = socket(AF_INET,SOCK_STREAM) client.connect((‘127.0.0.1‘,8081)) while True: msg = input(‘p;ease:‘).strip() if not msg:continue client.send(msg.encode(‘utf-8‘)) data = client.recv(1024) print(data.decode(‘utf-8‘))
标签:pre 列表 connect imp lis 数据 art false 客户端
原文地址:http://www.cnblogs.com/52-qq/p/7464692.html