select監聽客戶端

# coding: utf-8import socket# 定義一個消息列表messages = ['This is the message ', 'It will be sent ', 'in parts ', ]# 定義一個元祖,指定想要鏈接的服務端。server_address = ('localhost', 8090)# Create aTCP/IP socket# 初始化兩個socket# Connect thesocket to the port where the server is listening# 打印出要鏈接的IP和端口# 而後進行鏈接到服務器。# 鏈接到服務器for i in range(10):    socks = [socket.socket(socket.AF_INET, socket.SOCK_STREAM), socket.socket(socket.AF_INET, socket.SOCK_STREAM)]    print('connecting to %s port %s' % server_address)    for s in socks:        print(s)        s.connect(server_address)    # 循環遍歷,進行發送消息    for index, message in enumerate(messages):        # Send messages on both sockets        # 遍歷定義的socket,而後進行發送定義好的消息。        for s in socks:            print('%s: sending "%s"' % (s.getsockname(), message + str(index)))            s.send(bytes((message + str(index)).encode('utf-8')))        # Read responses on both sockets    # 進行接收消息,    for s in socks:        # 循環進行接收消息        data = s.recv(1024)        # 打印出接收的IP,端口,和接收到的消息。        print('%s: received "%s"' % (s.getsockname(), data))        if data != "":            print('closingsocket', s.getsockname())            s.close()
相關文章
相關標籤/搜索