print os.system () in python

1

I was practicing a small program in socket that allows access to the shell of the server machine. The case is that the p r int that would have to be sent to the client (which is the answer of os.system() ) are numeric (for example the clear is 0.

Does anyone know why or the solution?

The client code:

import socket

s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
try:
    s.connect(("192.168.43.241" , 4004))
    print "conexion establecida"

while True:
    mensaje = raw_input(">>")
    s.send(mensaje)
    print s.recv(1024)
except KeyboardInterrupt:
    exit()

the server code:

import socket

print "INICANDO SERVIDOR..."

s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)

try:
    s.bind(("192.168.43.241" , 4004))
    s.listen(5)
    sockets , data = s.accept()
    print str("conexion establecida")   
    while True:
        mensaje = socket.recv(1024)
    try:
        comando = os.system(mensaje)
    except:
        socket.send("Comando invalido")
    socket.send(comando)
 except KeyboardInterrupt:
     exit()
    
asked by angrymasther 01.06.2017 в 09:52
source

1 answer

1

Because os.system(mensaje) returns an "EXIT CODE", normally 0 if the command worked correctly, what you really want is to redirect the output of the command to socket.send , one way to do it is like this:

import subprocess

proc = subprocess.Popen(comando, stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
socket.send(out)
    
answered by 01.06.2017 / 16:22
source