I am testing sending a number of simple HTTP requests to an external system. While I have successfully shown that Functionally the system under test is working with a single message when I send more then 20 I am getting failures that seem to be due to the messages not being recieved rather then problems with the system itself.
Currently I am sending my messages via UDP but want to see if this is causing the drop out and so want to convert to sending via TCP
Code to send via 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()
Code to send Via TCP (so far)
@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()
As you can see above I have changed al references to UDP to TCP and converted Sendto to Send after reading online this is the proper way to send via TCP. However there seems to be something else missing here as it is failing to send anything now.
Any help as to the best way to convert the First code block to allow it to send TCP instead of UDP apprecitated
Sockets in python is modeled after linux sockets API ( Check the link to know more about differences in socket types and what linux APIs say).
Also it looks you didn't read the documentation of sockets in python, so read it
Now for you question, your issue is as following, in both examples you use socket.SOCK_DGRAM
which stands for data-gram. It's used for connection-less protocols like UDP.
For TCP, you need a socket type that supports two-way reliable ( you will know if a packet is lost ) which is SOCK_STREAM
. But because it's reliable, it must be stateful and can't be connection-less.
So you have to
SOCK_STREAM
type. # 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.