[SOLUCIONADO] error cuando ejecuto un comando

Iniciado por system20100, Septiembre 27, 2019, 09:07:27 PM

Tema anterior - Siguiente tema

0 Miembros y 2 Visitantes están viendo este tema.

Septiembre 27, 2019, 09:07:27 PM Ultima modificación: Noviembre 07, 2019, 10:07:00 PM por xyz
tengo un cliente con un servidor  se conectan pero cuando mando un comando dir no me funciona y me estos errores
Código: text

Código:
error cuando ejecuto el comando dir

corriendo servidor y esperando conexiones
conexion recibida de:127.0.0.1
()-#:dir
Traceback (most recent call last):
  File "server.py", line 34, in <module>
    shell()
  File "server.py", line 15, in shell
    res=targer.recv(1024)
socket.error: [Errno 10053] Se ha anulado una conexi¾n establecida por el software en su equipo host



Código: text

servidor
Código:
#!/usr/bin/env python
#_*_ coding: utf8_*_

import socket

def shell():
    current_dir=targer.recv(1024)
    while True:
        comando=raw_input("()-#:".format(current_dir))
        if comando == "exit":
            targer.send(comando)
            break
        else:
            targer.send(comando)
            res=targer.recv(1024)
            print(res)
   
def upserver():
    global server #variables globales
    global ip
    global targer
   
    server=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind(('localhost', 7777))# lo ponemos a la escucha con un puerto
    server.listen(1)
   
    print("corriendo servidor y esperando conexiones")
   
    targer, ip=server.accept() #recibimos un objete de la conexion y una lista de datos
    print("conexion recibida de:"+ str(ip[0]))
   
upserver()
shell()
server.close()


Código: text

cliente
Código:
#!/usr/bin/env python
#_*_ coding: utf8_*_


import socket
import os
import subprocess

def shell():
    current_dir=os.getcwd()
    cliente.send(current_dir)
    while True:
        res=cliente.recv(1024)
        if res == "exit":
            break
        else:
            proc=subprocess.Popen(res, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            resul=proc.stdout.read() + proc.stderr.read()
            cliente.send(resul)
    re=cliente.recv(1024)
    print(res)
    cliente.send('hola mundo')

cliente=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cliente.connect(('localhost',7777))
cliente.close

Septiembre 27, 2019, 10:03:58 PM #1 Ultima modificación: Septiembre 27, 2019, 10:26:00 PM por DtxdF
@You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login

Noté que hay varios errores en tu código (Solo uno, puede afectar el funcionamiento), sin embargo no es el origen del problema.

El primero fue

Código: python
comando=raw_input("()-#:".format(current_dir))


Se coloca corchetes ('{}'), no paréntesis ('()').

El segundo:

Código: python
cliente.close


No ejecutas el método 'close', tienes que colocarlo de la siguiente manera:

Código: python
cliente.close()


Por último y puede que sea el problema que no te permita obtener datos. Es que nunca ejecutas la función 'shell()'.

- DtxdF
PGP :: <D82F366940155CB043147178C4E075FC4403BDDC>

~ DtxdF