簡體   English   中英

將方法更改為通過UDP發送改為通過TCP發送

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

我正在測試向外部系統發送一些簡單的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中的套接字是根據linux套接字API建模的(請查看鏈接以了解有關套接字類型和linux API說什么的更多信息)。

此外,它看起來你沒看過python中插槽的文檔,所以閱讀

現在,您遇到的問題是,您的問題如下,在兩個示例中,您都使用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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM