0

I started writing a primitive ssh-like program for practice's sake. I was starting to check if the commands would be delivered properly before implementing any further functionality, so i just wrote a server which receives messages from the client and prints them in it's own console window, but before it receives a message, it sends a prefix to the client, which the client prints so it's right before ( to the left of) the user input. For the prefix to be to the left of the input i tried to use print( ... , end=''), but that seems to break the whole thing, and the prefix then does not print at all, as opposed to the solution where the prefix is printed the line before, where the program works perfectly fine besides that little problem.

TL;DR :

this (where s is the socket)

for n, line in enumerate(sys.stdin):
    prefix = s.recv(16)
    print(prefix.decode('utf-8'), end='')
    s.send(line.encode('utf-8'))

code does not print the received prefix at all, only after closing the program with CTRL + D or CTRL + C as shown below:

czyngis@czyngis-mint:[directory censored]$ python3 cmd_client.py -t fc70:6872:4c6:10ae:71c::1 -p 9999
target: fc70:6872:4c6:10ae:71c::1 | port: 9999
connection established with: fc70:6872:4c6:10ae:71c::1 on port: 9999

first code sample  //< user input
does not work :(   //< user input
^C<#CMD:><#CMD:><#CMD:>Traceback (most recent call last):
  File "cmd_client.py", line 26, in <module>
    for n, line in enumerate(sys.stdin):
KeyboardInterrupt

and this ( where s is also the socket )

for n, line in enumerate(sys.stdin):
    prefix = s.recv(16)
    print(prefix.decode('utf-8'))
    s.send(line.encode('utf-8'))

code does print the prefix, but in the previous line, as shown below:

czyngis@czyngis-mint:[directory censored]$ python3 cmd_client.py -t fc70:6872:4c6:10ae:71c::1 -p 9999
target: fc70:6872:4c6:10ae:71c::1 | port: 9999
connection established with: fc70:6872:4c6:10ae:71c::1 on port: 9999

<#CMD:>
second code sample
<#CMD:>
works, but not quite :(^CTraceback (most recent call last):
  File "cmd_client.py", line 26, in <module>
    for n, line in enumerate(sys.stdin):
KeyboardInterrupt

Thank You for any help in advance.

czyngis
  • 413
  • 2
  • 5
  • 18
  • See in particular the answer on the linked duplicate at https://stackoverflow.com/a/14729823/14122 – Charles Duffy Jan 17 '20 at 14:59
  • @CharlesDuffy Thank you very much. Lightning fast response, very much appreciated. Works perfect. I just have one little question if you do not mind: with this code i wrote, even though the first action in the loop is receiving the prefix from server and printing it, before that happends the client has to enter one message first, without the prefix behind it, and only then the s.recv() and print() is executed after the message is entered, yet i want the prefix to be there on the first message aswell. Is there a way to solve this somehow? – czyngis Jan 17 '20 at 15:05
  • Hmm. That means restructuring the logic a bit. Of the top of my head, I almost wonder if it would be appropriate to use something like `itertools.chain([(-1, ''), enumerate(sys.stdin)]` as the subject of your `for` loop, adding a first element before anything is read from stdin at all -- but honestly, I'd ask that as its own separate question if you can't find an existing knowledge-base entry covering it. – Charles Duffy Jan 17 '20 at 16:24
  • Looking back at that, I see I got the code wrong a bit. `itertools.chain([(-1, '')], enumerate(sys.stdin))` is closer to what I meant to propose. – Charles Duffy Jan 18 '20 at 12:33

0 Answers0