将通过 UDP 发送的方法更改为通过 TCP 发送
Changing a Method to send via UDP to instead send via TCP
我正在测试向外部系统发送大量简单的 HTTP 请求。虽然我已经成功地表明,当我发送超过 20 条消息时,被测系统在功能上正在处理一条消息,但我遇到的失败似乎是由于未收到消息而不是系统本身的问题。
目前我正在通过 UDP 发送消息,但想看看这是否会导致丢失,因此想转换为通过 TCP 发送
通过 UDP 发送的代码
@when('{number_of_requests} HTTP requests are sent to it')
def step_impl(context, number_of_requests):
# Waiting for the service to be ready after restart
time.sleep(1)
# Get the Endpoint address and port from Config.py
UDP_IP = config.endpoint['host']
UDP_PORT = int(config.endpoint['port'])
context.messages_sent = []
# Setting up the socket to send the UDP datagrams
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(True)
# Getting the configured sub id header name
sub_id_header_name = get_subid_header_name()
context.subscribers = generate_subscribers(number_of_requests)
sock.sendto(bytes("xxx", "utf-8"), (UDP_IP, UDP_PORT))
for i in range(int(number_of_requests)):
# Generating a SPID
spid = encodespid("ab")
# Generating a SUB_ID
sub_id = context.subscribers[i]
# Setting up the whole message to be sent
message = small_message.format(spid, sub_id_header_name, sub_id)
# Sending the message
sock.sendto(bytes(message, "utf-8"), (UDP_IP, UDP_PORT))
context.messages_sent.append(spid)
# Closing the socket
sock.close()
通过 TCP 发送的代码(到目前为止)
@when('{number_of_requests} HTTP requests are sent to it')
def step_impl(context, number_of_requests):
# Waiting for the service to be ready after restart
time.sleep(1)
# Get the Endpoint address and port from Config.py
TCP_IP = config.endpoint['host']
TCP_PORT = int(config.endpoint['port'])
context.messages_sent = []
# Setting up the socket to send the UDP datagrams
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(True)
# Getting the configured sub id header name
sub_id_header_name = get_subid_header_name()
context.subscribers = generate_subscribers(number_of_requests)
sock.send(bytes("xxx", "utf-8"), (TCP_IP, TCP_PORT))
for i in range(int(number_of_requests)):
# Generating a SPID
spid = encodespid("ab")
# Generating a SUB_ID
sub_id = context.subscribers[i]
# Setting up the whole message to be sent
message = small_message.format(spid, sub_id_header_name, sub_id)
# Sending the message
sock.send(bytes(message, "utf-8"), (TCP_IP, TCP_PORT))
context.messages_sent.append(spid)
# Closing the socket
sock.close()
正如您在上面看到的,我已经将所有对 UDP 的引用更改为 TCP,并将 Sendto 转换为 Send,这是通过 TCP 发送的正确方法。但是,这里似乎还缺少其他内容,因为它现在无法发送任何内容。
关于转换第一个代码块以允许它发送 TCP 而不是 UDP 的最佳方式的任何帮助表示赞赏
python 中的套接字是在 linux 套接字 API 之后建模的(检查 link 以了解更多关于套接字类型的差异以及 linux API说)。
此外,您似乎没有阅读 python 中的套接字文档,因此请阅读 it
现在你的问题,
您的问题如下,在这两个示例中,您都使用 socket.SOCK_DGRAM
代表数据语法。它用于 UDP 等无连接协议。
对于TCP,你需要一个支持双向可靠的套接字类型(你会知道是否有数据包丢失),即SOCK_STREAM
。但是因为可靠,所以一定是有状态的,不能是无连接的。
所以你必须
- 创建
SOCK_STREAM
类型的套接字。
- 创建到服务器的连接。
- send/receive.
- 关闭连接
# create socket of SOCK_STREAM type
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to your server
sock.connect((TCP_IP, TCP_PORT))
# do the sending/receiving part
sock.sendall(b'Hello, world')
data = sock.recv(1024)
# close the connection
s.close()
我正在测试向外部系统发送大量简单的 HTTP 请求。虽然我已经成功地表明,当我发送超过 20 条消息时,被测系统在功能上正在处理一条消息,但我遇到的失败似乎是由于未收到消息而不是系统本身的问题。
目前我正在通过 UDP 发送消息,但想看看这是否会导致丢失,因此想转换为通过 TCP 发送
通过 UDP 发送的代码
@when('{number_of_requests} HTTP requests are sent to it')
def step_impl(context, number_of_requests):
# Waiting for the service to be ready after restart
time.sleep(1)
# Get the Endpoint address and port from Config.py
UDP_IP = config.endpoint['host']
UDP_PORT = int(config.endpoint['port'])
context.messages_sent = []
# Setting up the socket to send the UDP datagrams
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(True)
# Getting the configured sub id header name
sub_id_header_name = get_subid_header_name()
context.subscribers = generate_subscribers(number_of_requests)
sock.sendto(bytes("xxx", "utf-8"), (UDP_IP, UDP_PORT))
for i in range(int(number_of_requests)):
# Generating a SPID
spid = encodespid("ab")
# Generating a SUB_ID
sub_id = context.subscribers[i]
# Setting up the whole message to be sent
message = small_message.format(spid, sub_id_header_name, sub_id)
# Sending the message
sock.sendto(bytes(message, "utf-8"), (UDP_IP, UDP_PORT))
context.messages_sent.append(spid)
# Closing the socket
sock.close()
通过 TCP 发送的代码(到目前为止)
@when('{number_of_requests} HTTP requests are sent to it')
def step_impl(context, number_of_requests):
# Waiting for the service to be ready after restart
time.sleep(1)
# Get the Endpoint address and port from Config.py
TCP_IP = config.endpoint['host']
TCP_PORT = int(config.endpoint['port'])
context.messages_sent = []
# Setting up the socket to send the UDP datagrams
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(True)
# Getting the configured sub id header name
sub_id_header_name = get_subid_header_name()
context.subscribers = generate_subscribers(number_of_requests)
sock.send(bytes("xxx", "utf-8"), (TCP_IP, TCP_PORT))
for i in range(int(number_of_requests)):
# Generating a SPID
spid = encodespid("ab")
# Generating a SUB_ID
sub_id = context.subscribers[i]
# Setting up the whole message to be sent
message = small_message.format(spid, sub_id_header_name, sub_id)
# Sending the message
sock.send(bytes(message, "utf-8"), (TCP_IP, TCP_PORT))
context.messages_sent.append(spid)
# Closing the socket
sock.close()
正如您在上面看到的,我已经将所有对 UDP 的引用更改为 TCP,并将 Sendto 转换为 Send,这是通过 TCP 发送的正确方法。但是,这里似乎还缺少其他内容,因为它现在无法发送任何内容。
关于转换第一个代码块以允许它发送 TCP 而不是 UDP 的最佳方式的任何帮助表示赞赏
python 中的套接字是在 linux 套接字 API 之后建模的(检查 link 以了解更多关于套接字类型的差异以及 linux API说)。
此外,您似乎没有阅读 python 中的套接字文档,因此请阅读 it
现在你的问题,
您的问题如下,在这两个示例中,您都使用 socket.SOCK_DGRAM
代表数据语法。它用于 UDP 等无连接协议。
对于TCP,你需要一个支持双向可靠的套接字类型(你会知道是否有数据包丢失),即SOCK_STREAM
。但是因为可靠,所以一定是有状态的,不能是无连接的。
所以你必须
- 创建
SOCK_STREAM
类型的套接字。 - 创建到服务器的连接。
- send/receive.
- 关闭连接
# create socket of SOCK_STREAM type
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to your server
sock.connect((TCP_IP, TCP_PORT))
# do the sending/receiving part
sock.sendall(b'Hello, world')
data = sock.recv(1024)
# close the connection
s.close()