Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将通过UDP发送的方法改为通过TCP发送_Python_Tcp_Udp - Fatal编程技术网

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

Python 将通过UDP发送的方法改为通过TCP发送,python,tcp,udp,Python,Tcp,Udp,我正在测试向外部系统发送一些简单的HTTP请求。虽然我已经成功地证明了在功能上,当我发送超过20条消息时,被测系统正在处理一条消息,但我得到的失败似乎是由于没有接收到消息,而不是由于系统本身的问题 目前,我正在通过UDP发送消息,但我想知道这是否会导致退出,因此希望转换为通过TCP发送 要通过UDP发送的代码 @when('{number_of_requests} HTTP requests are sent to it') def step_impl(context, number_of_re

我正在测试向外部系统发送一些简单的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发送的代码(到目前为止)

正如您在上面看到的,我已经将所有对UDP的引用更改为TCP,并在联机阅读后将Sendto转换为Send。这是通过TCP发送的正确方式。然而,这里似乎还缺少一些东西,因为它现在没有发送任何东西


任何关于转换第一个代码块以允许它发送TCP而不是python中指定的UDP的最佳方法的帮助都是以linux套接字为模型的(查看链接以了解套接字类型的差异以及linux API所说的内容)

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

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

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

所以你必须

  • 使用
    SOCK\u流
    类型创建套接字
  • 创建到服务器的连接
  • 发送/接收
  • 关闭连接

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