02select监听客户端

02select监听客户端[Python基础]

# coding: utf-8
import 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()

以上是 02select监听客户端 的全部内容, 来源链接: utcz.com/z/538041.html

回到顶部