Python 3下基于select模型的事件驱动机制程序(3)

import socket messages = ['This is the message ', 'It will be sent ', 'in parts ', ] server_address = ('localhost', 8090) # Create aTCP/IP socket socks = [socket.socket(socket.AF_INET, socket.SOCK_STREAM), socket.socket(socket.AF_INET, socket.SOCK_STREAM), ] # Connect thesocket to the port where the server is listening print('connecting to %s port %s' % server_address) # 连接到服务器 for s in socks: s.connect(server_address) for index, message in enumerate(messages): # Send messages on both sockets for s in socks: print('%s: sending "%s"' % (s.getsockname(), message + str(index))) send_data = message + str(index) s.sendall(bytes(send_data, encoding='utf-8')) # Read responses on both sockets for s in socks: data = s.recv(1024) print('%s: received "%s"' % (s.getsockname(), data)) if data != "": print('closingsocket', s.getsockname()) s.close()

从结果可以看到, 客户端会发送空字符过来, 查了下资料, 这是客户端在确定是否断开, 服务器端加个判断就行。
运行截图:

Python 3下基于select模型的事件驱动机制程序

Python 3下基于select模型的事件驱动机制程序

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/c5f837210c17a59fc7d32698dbf2a7f6.html