首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将要通过UDP发送的方法改为通过TCP发送

问将要通过UDP发送的方法改为通过TCP发送
EN

Stack Overflow用户
提问于 2018-07-23 11:14:05
回答 1查看 81关注 0票数 0

我正在测试向外部系统发送一些简单的HTTP请求。虽然我成功地证明了被测试的系统在功能上使用单个消息,当我发送超过20条消息时,我遇到的失败似乎是由于没有接收到的信息,而不是系统本身的问题。

目前,我正在通过UDP发送我的消息,但我想看看这是否导致了退出,因此我想转换为通过TCP发送。

通过UDP发送的代码

代码语言:javascript
复制
@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发送的代码(到目前为止)

代码语言:javascript
复制
@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转换为在线阅读后发送,这是通过TCP发送的正确方法。然而,这里似乎缺少了其他东西,因为它现在没有发送任何东西。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-23 11:55:32

python中的套接字是以linux (查看链接了解更多关于套接字类型的差异和linux )为模型的。

另外,您似乎没有阅读python中的套接字文档,所以请阅读它

现在,对于您的问题,您的问题如下所示,在这两个示例中,您都使用socket.SOCK_DGRAM表示数据记录。它用于像UDP这样的无连接协议。

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

所以你必须

  1. 使用SOCK_STREAM类型创建套接字。
  2. 创建到服务器的连接。
  3. 发送/接收。
  4. 关闭连接
代码语言:javascript
复制
# 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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51477487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档