简体   繁体   English

python3 socket send recev 'bytes' object 没有属性 'read'

[英]python3 socket send recev 'bytes' object has no attribute 'read'

I have two files: client.py and server.py that when run are connected by socket.我有两个文件: client.pyserver.py ,它们在运行时通过套接字连接。
When I send a command to the client from the server, for example a simple ls , I use a function (in the client) called subprocess.Popen to execute it in the shell.当我从服务器向客户端发送命令时,例如一个简单的ls ,我使用名为 subprocess.Popen 的 function(在客户端)在subprocess.Popen中执行它。 However, the error bytes object has no attribute 'read' appears to me.但是,错误字节object has no attribute 'read'

Is there any other way to execute a command other than the subprocess module?除了子进程模块之外,还有其他方法可以执行命令吗?

Am I running subprocess correctly with the following call:我是否通过以下调用正确运行子进程:

command = subprocess.Popen (args, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)

Below I attached the code of the two programs.下面我附上了这两个程序的代码。

The server code is as follows:服务器代码如下:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import socket

def main():
    try:
        servidor = socket.socket()
        servidor.bind(('localhost',7777))
        servidor.listen(1)

        while True:
            client, direccion = servidor.accept()
            print('[+] Conexion de: {}'.format(direccion))

            while True:
                comando = input("<server>: ")
                client.send(comando.encode())
                result = client.recv(4096)
                print(result.decode())
    except Exception as e:
        print (e)

if __name__ == '__main__' :
    try:
        main()
    except KeyboardInterrupt:
        exit()

The client code is as follows:客户端代码如下:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import socket
import subprocess
import shlex


def main():
    try:
        client = socket.socket()
        client.connect(('localhost', 7777))

        while True:
            datos = client.recv(4096)
            args = shlex.split(datos)
            comando = subprocess.Popen(args, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
            
            if comando.stderr.read() != "":
                client.send("[-] Error de comando")
            else:
                cliente.send(comando.stdout.read())
    except Exception as e: 
        print (e)

if __name__ == '__main__' :
    try:    
        main()
    except KeyboardInterrupt:
        exit()
   args = shlex.split(datos)

datos is of type bytes , but shlex.split expects a str . datos 是bytes类型,但shlex.split需要一个str

   args = shlex.split(datos.encode())

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

相关问题 Python3 'bytes' object 中的 HMAC-SHA1 没有属性 'encode' - HMAC-SHA1 in Python3 'bytes' object has no attribute 'encode' AttributeError: 'function' object 没有属性 'read_excel' python3 - AttributeError: 'function' object has no attribute 'read_excel' python3 Python; urllib 错误:AttributeError: 'bytes' object 没有属性 'read' - Python; urllib error: AttributeError: 'bytes' object has no attribute 'read' AttributeError: 'bytes' object 在 Python Sockets 库上没有属性 'read' - AttributeError: 'bytes' object has no attribute 'read' on Python Sockets Library AttributeError: 'bytes' object 没有属性 'read' - AttributeError: 'bytes' object has no attribute 'read' &#39;str&#39; 对象在 Python3 中没有属性 &#39;decode&#39; - 'str' object has no attribute 'decode' in Python3 对象在python3中没有属性&#39;.__ dict__&#39; - Object has no attribute '.__dict__' in python3 Python 错误 &#39;bytes&#39; 对象没有属性 &#39;encode&#39; - Python error 'bytes' object has no attribute 'encode' “模块”对象没有属性“ to_bytes” Python - 'module' object has no attribute 'to_bytes' Python AttributeError: 'bytes' object 从 python 中的 GCS 读取.gz 文件时没有属性 'read' - AttributeError: 'bytes' object has no attribute 'read' while reading .gz file from GCS in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM