【问题标题】:Changing a Method to send via UDP to instead send via TCP将通过 UDP 发送的方法更改为通过 TCP 发送
【发布时间】:2018-07-23 19:55:32
【问题描述】:

我正在测试向外部系统发送一些简单的 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 tcp udp


    【解决方案1】:

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

    另外看来你没有阅读 python 中的套接字文档,请阅读it

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

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

    所以你必须

    1. 使用SOCK_STREAM 类型创建套接字。
    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()
    

    【讨论】:

      猜你喜欢
      • 2018-06-28
      • 2018-11-09
      • 2014-07-13
      • 1970-01-01
      • 2017-01-25
      • 2014-01-16
      • 2016-02-20
      • 2016-03-02
      • 1970-01-01
      相关资源
      最近更新 更多