how to make the system call return to the inverted string?

Asked

Viewed 74 times

-1

print('Listening on {} {}'.format(ip,porta))
     (obj, cliente) = server.accept()
     print('connect received from {}'.format(cliente[0]))
    while (True):
        msg = obj.recv(1024)
        **system('rev')**
        server.close()
  • What exactly should the program do? What are you currently doing?

  • well, I must assemble a server that calls the Linux rev command and returns it to the user.

  • 1

    Why not print(msg[::-1])? https://stackoverflow.com/questions/931092/reverse-a-string-in-python?answertab=votes#tab-top

  • so I would like the linux utility to do this work for me without using the language resources.

  • https://stackoverflow.com/questions/89228/calling-an-external-command-in-python?answertab=votes#tab-top

  • import socket from os import system ip = '0.0.0' port = 663 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((ip, port) server.Listen(5) print('Listening on {} {}'.format(ip, port) (obj, client) = server.Accept() print('connect Received from {}'.format(client[0])) while (True): msg = obj.recv(1024) msg = [:-1] system('rev') server.close() except: print('error') server.close()

  • would not like to use language features but rather the linux rev utility, so I would like to make a system call and it does this work for me, thanks for the help and sorry some error, my first time here!

  • 1

    A tip: instead of putting code (and any other additional information) in the comments, it is better to [Dit] your question and put everything there. In addition to concentrating all relevant information in one place, the question is possible format the code and make it more readable than in the comments. Editing posts is a common practice on the site, I suggest you read the help center also for more information on the operation of the website.

Show 3 more comments

1 answer

0


Use the command rev with | bash:

echo 12345|rev

Returns:

54321

That is to say: **system('{comando que retorna string}|rev')**

  • then, I added echo , system('{echo}|rev'), how to pass the variable with the text as parameter to the system call? this is my biggest doubt, because when I connect to the server and call the rev it is active on the server! , then I have to go there and type the message, I would like to receive from the user and pass the variable as parameter to the system call!

  • So {} is just an example, but you can also do: **system('programa {}|rev'.format(texto_como_param))**

Browser other questions tagged

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