# 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 = https://www.cnblogs.com/cong12586/p/s.recv(1024)
# 列印出接收的IP,埠,和接收到的訊息,
print('%s: received "%s"' % (s.getsockname(), data))
if data != "":
print('closingsocket', s.getsockname())
s.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/228397.html
標籤:Python
