python-3.x - Python socket recv 没有给出好的结果

标签 python-3.x sockets networking

我正在尝试为我的 IT 类(class)构建一个程序。该程序的重​​点是让客户端应用程序向服务器发送命令。它似乎工作得很好,直到今天,经过几次通话,当我收到来自服务器的响应时,它不是最新的。
例如:我发送了一些一切正常的命令。但随后发送另一个命令并接收前一个命令的响应。
我检查了客户端发送的命令,它是我键入的命令,在服务器部分,当我从客户端收到命令时,它是客户端实际发送的命令(不是前一个)
这是我用来发送和接收消息的 Shell 类(在服务器和客户端中)以及我如何使用它的示例。
服务器 :

class Shell:
command = ""
next_command = True

def __init__(self, malware_os):
    self._os = malware_os
    self._response = ""

def receive(self):
    self.command = distant_socket.recv(4096).decode("utf-8")

def execute_command(self):
    if self.command[:2] == "cd":
        os.chdir(self.command[3:])
        if self._os == "Windows":
            self.result = Popen("cd", shell=True, stdout=PIPE)
        else:
            self.result = Popen("pwd", shell=True, stdout=PIPE)
    else:
        self.result = Popen(self.command, shell=True, stdout=PIPE)

    self._response = self.result.communicate()

def send(self):
    self._response = self._response[0]
    self._response = self._response.decode("utf-8", errors="ignore")
    self._response = self._response + " "
    self._response = self._response.encode("utf-8")
    distant_socket.send(self._response)
    self._response = None
在服务器中使用:
    shell.receive()
    shell.execute_command()
    shell.send()
客户 :
class Shell:

    def __init__(self):
        self._history = []
        self._command = ""

    def send(self):
        self._history.append(self._command)
        s.send(self._command.encode("utf-8"))

    def receive(self):
        content = s.recv(4096).decode("utf-8", errors="ignore")
        if content[2:] == "cd":
            malware_os.chdir(self._command[3:].decode("utf-8", errors="ignore"))
        print(content)

    def history(self):
        print("The history of your commands is:")
        print("----------------------")
        for element in self._history:
            print(element)

    def get_command(self):
        return self._command

    def set_command(self, command):
        self._command = command
在客户端使用:
shell.set_command(getinfo.get_users())
shell.send()
shell.receive()
预先感谢您的帮助,
亲切地,
大脚野人

最佳答案

既然您说响应不是最新的,我猜您使用了 TCP(您没有发布套接字创建)。就像提到的评论一样,有两件事你做的不对:

  • 协议(protocol):TCP 为您提供一个流,按照操作系统认为适合的数据包进行划分。通过网络传输数据时,接收端必须知道何时完成传输。最简单的方法是在传输本身之前以固定格式(比如 4 个字节,大端)发送传输的长度。另外,使用 sendall。例如:

  • import struct
    def send_message(sock, message_str):
        message_bytes = message_str.encode("utf-8")
        size_prefix = struct.pack("!I", len(message_bytes)) # I means 4 bytes integer in big endian
        sock.sendall(size_prefix)
        sock.sendall(message_bytes)
    
  • 由于 TCP 是一个流套接字,因此接收端可能会在接收到整个消息之前从 recv 返回。您需要在循环中调用它,在每次迭代时检查返回值以正确处理断开连接。例如:

  • def recv_message_str(sock):
        #first, get the message size, assuming you used the send above
        size_buffer = b""
        while len(size_buffer) != 4:
            recv_ret = sock.recv(4 - len(size_buffer))
            if len(recv_ret) == 0:
                # The other side disconnected, do something (raise an exception or something)
                raise Exception("socket disconnected")
            size_buffer += recv_ret
        size = struct.unpack("!I", size_buffer)[0]
        
        # Loop again, for the message string
        message_buffer = b""
        while len(message_buffer) != size:
            recv_ret = sock.recv(size - len(message_buffer))
            if len(recv_ret) == 0:
                # The other side disconnected, do something (raise an exception or something)
                raise Exception("socket disconnected")
            message_buffer += recv_ret
        return message_buffer.decode("utf-8", errors="ignore")
    

    关于python-3.x - Python socket recv 没有给出好的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63217805/

    相关文章:

    python3迭代地从字符串中删除第一个字符

    python-3.x - 编辑图层中的各个权重和偏差

    python - 使用python发送unsigned char

    node.js - 从外部应用程序连接到Docker中的MongoDB

    java - 确定用于 Java RMI 调用的远程客户端 IP 地址

    python-3.x - 共享点文件夹应用程序权限 - 未经授权

    python - 如何查找给定数据框的多列之间的差异并将结果保存为单独的数据框

    c++ - 确定未绑定(bind)套接字的地址族

    c - 套接字接收后的 Memcpy 不起作用

    c# - 每 60 秒向 RoR 应用程序发送一次 http put 请求的问题