简体   繁体   English

将方法更改为通过UDP发送改为通过TCP发送

[英]Changing a Method to send via UDP to instead send via TCP

I am testing sending a number of simple HTTP requests to an external system. 我正在测试向外部系统发送一些简单的HTTP请求。 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. 虽然我已经成功地表明在功能上,当我发送20多个消息时,被测系统正在处理一条消息,但我遇到的故障似乎是由于未收到消息,而是系统本身出现了问题。

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 目前,我正在通过UDP发送消息,但想查看是否这会导致退出,因此想转换为通过TCP发送

Code to send via UDP 通过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) 通过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()

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. 正如您在上面看到的那样,在线阅读后,我已将对UDP的所有引用更改为TCP,并将Sendto转换为Send,这是通过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 关于转换第一个代码块以使其能够发送TCP而不是UDP的最佳方法的任何帮助

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). python中的套接字是根据linux套接字API建模的(请查看链接以了解有关套接字类型和linux API说什么的更多信息)。

Also it looks you didn't read the documentation of sockets in python, so read it 此外,它看起来你没看过python中插槽的文档,所以阅读

Now for you question, your issue is as following, in both examples you use socket.SOCK_DGRAM which stands for data-gram. 现在,您遇到的问题是,您的问题如下,在两个示例中,您都使用socket.SOCK_DGRAM代表数据报。 It's used for connection-less protocols like UDP. 它用于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 . 对于TCP,您需要一个支持双向可靠的套接字类型(您将知道数据包是否丢失),即SOCK_STREAM But because it's reliable, it must be stateful and can't be connection-less. 但是因为它是可靠的,所以它必须是有状态的,并且不能没有连接。

So you have to 所以你必须

  1. create socket with SOCK_STREAM type. 创建具有SOCK_STREAM类型的套接字。
  2. create a connection to the server. 创建与服务器的连接。
  3. send/receive. 发送/接收。
  4. close the connection 关闭连接

# 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM