[英]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.py和server.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.