(Socket) Client and Server Connection - how to get Cmd-> Client (Python) return

Asked

Viewed 166 times

2

I want to execute a command in the client’s Cmd and return the values to the server. I Already Managed to Execute the command in the client’s Cmd... Now how do I return the values to the server?

The Server:

import socket
import os


Host = "127.0.0.1" #str(input("Host: "))
Door = 8291        #int(input("Door: "))

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

conexao.bind((Host,Door))
conexao.listen(2)

c, e = conexao.accept()

while True:

    os.system('cls')

    print("Connected to : ", e)

    print("\n\n")

    print("|----------------|-------|")
    print("|  Instructions  | Press |")
    print("|----------------|-------|")
    print("|  Send Message  |   1   |")
    print("|----------------|-------|")
    print("|  Send CodeDos  |   2   |")
    print("|----------------|-------|")

    print("\n\n")

    Choice = int(input("I Pick: "))

    os.system("cls")

    if Choice == 1:

        Msg = input("Message:")
        c.send("1".encode('ascii'))
        c.send(Msg.encode('ascii'))


    elif Choice == 2:

        Cod = input("CodeDos:")
        c.send("2".encode('ascii'))
        c.send(Cod.encode('ascii'))


    else:

        print("Faill")
        os.system('pause')

The client:

import socket
import os


Host = "127.0.0.1" #str(input("Host: "))
Door = 8291        #int(input("Door: "))

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

conexao.connect((Host,Door))

while True:

    info = conexao.recv(1024)

    if info.decode('ascii') == "1":   # If Choice == 1 (Message) Do:

        info = conexao.recv(1024)

        info = info.decode('ascii')

        print(info)

    elif info.decode('ascii') == "2": # If Choice == 2 (CodeDos) Do:

        info = conexao.recv(1024)

        info = info.decode('ascii')

        os.system(info)
  • In your 'client' you only used the receipt with "recv", but, did not send again to give a return! One can implement sending at the end of "while" which will return!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.