代码之家  ›  专栏  ›  技术社区  ›  Richard C

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

  •  0
  • Richard C  · 技术社区  · 8 年前

    我正在测试向外部系统发送一些简单的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,并将send转换为在线阅读后发送,这是通过tcp发送的正确方法。然而,这里似乎还有一些东西丢失了,因为它现在无法发送任何东西。

    关于转换第一个代码块以允许它发送TCP而不是UDP的最佳方法的任何帮助

    1 回复  |  直到 8 年前
        1
  •  1
  •   Shady Atef    8 年前

    python中的套接字是根据Linux套接字建模的。 API (检查链接,了解更多关于套接字类型的差异以及LinuxAPI所说的内容)。

    而且看起来您没有阅读python中套接字的文档,所以请阅读 it

    现在问你一个问题, 您的问题如下,在您使用的两个示例中 socket.SOCK_DGRAM 代表数据图。它用于像UDP这样的无连接协议。

    对于TCP,您需要支持双向可靠(您将知道数据包是否丢失)的套接字类型,这是 SOCK_STREAM . 但是因为它是可靠的,所以它必须是有状态的,并且不能是无连接的。

    所以你必须

    1. 使用创建套接字 袜子流 类型。
    2. 创建到服务器的连接。
    3. 发送/接收。
    4. 关闭连接

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