From 985739f7fbb2e2961b94707cd2ea93440425ea01 Mon Sep 17 00:00:00 2001 From: Al Bailey Date: Mon, 2 May 2022 15:24:43 +0000 Subject: [PATCH] Debian: Fix VIM API calls On python 2 struct.pack returns a string, and therefore a payload of type 'string' can be appended to it, before being converted to bytes for writing to tcp. On python 3 struct.pack returns bytes, and therefore a payload of type 'string' can not be appended to it. The payload needs to be converted to bytes first. The conversion of bytes to bytes (writing to tcp) is a no-op. This change has no impact on python2 code. Test Plan: PASS: "sw-manager patch-strategy show" does not generate exceptions in nfv-vim or nfv-vim-api logs on Debian. Closes-Bug: 1971171 Signed-off-by: Al Bailey Change-Id: Ia4fc56ca20f1e013ad1432802c4f18793ee3bb26 --- nfv/nfv-common/nfv_common/tcp/_tcp_connection.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/nfv/nfv-common/nfv_common/tcp/_tcp_connection.py b/nfv/nfv-common/nfv_common/tcp/_tcp_connection.py index 27680751..e3953f2f 100755 --- a/nfv/nfv-common/nfv_common/tcp/_tcp_connection.py +++ b/nfv/nfv-common/nfv_common/tcp/_tcp_connection.py @@ -8,6 +8,7 @@ import errno import hashlib import hmac import select +import six import socket import struct @@ -119,20 +120,27 @@ class TCPConnection(object): """ Send a message into the TCP connection, assumes the following messaging format: | length (4-bytes) | string of bytes | + struct.pack returns string in python2 and bytes in python3 """ bytes_sent = 0 if self._socket is not None: if self._auth_key is None: msg = struct.pack('!L', socket.htonl(len(payload))) - msg += payload + if six.PY3: + msg += bytes(payload, 'utf-8') + else: + msg += payload else: auth_vector = hmac.new(self._auth_key, msg=payload, digestmod=hashlib.sha512).digest() msg_len = len(auth_vector) + len(payload) msg = struct.pack('!L', socket.htonl(msg_len)) msg += auth_vector[:self.AUTH_VECTOR_MAX_SIZE] - msg += payload + if six.PY3: + msg += bytes(payload, 'utf-8') + else: + msg += payload bytes_sent = self._socket.send(bytes(msg)) return bytes_sent