Keep the console input line below the output

[EDIT:] I'm currently trying to create a small tcp chat application. Sending and receiving messages already works fine ... But the problem is that:

When I start typing a message while I receive it ... it appears after the text that I write

Screenshot: http://s7.directupload.net/images/140816/6svxo5ui.png

[The user sent> "hello", then began to write "I am writing ...", then the user wrote "I sent ..." before sending his message ... so that it was placed after my input ... I I want the incoming message to always be before my input!

this is my current code: Client.py

con = connect.User()
server = raw_input("Type in the server adress \n[leave blank to use xyr.no-ip.info]\n>:")
nick =""

while nick == "":
    nick = raw_input("Type in your nickname\n>:")

con.connect(server, nick)


def sender():
    print("Sender started")
    while 1:
        msg = raw_input()
        if msg == "q":
            break
        con.send(msg)

    con.disconnect()



def receiver(server):

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

        if server == "":
            server="xyr.no-ip.info"

        sock.connect((server, 8000))

        sock.send("%pyreceiver\n")

        print("Receiver started")

        while 1:
           msg_in = sock.recv(1024)
           if not str(msg_in).startswith("[py]" + nick):
               if str(msg_in).startswith("/ua"):
                   print(str(msg_in)[3:])

               elif str(msg_in).startswith("/u "):
                   print(str(msg_in)[2:])
               else:

                print(str(msg_in[:-1]))
#

if nick == "":
    nick = "guest"
    print("Name changed to ""guest""")
    time.sleep(.5)

thread.start_new_thread(receiver, (server, ))
time.sleep(.5)
thread.start_new_thread(sender())

Connect.py

import socket

import time

class User():


    nickel =""
    def connect(self, server="xyr.no-ip.info", nick="guest"):
        nickel = nick
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if server == "":
            server="xyr.no-ip.info"
            print("server changed to xyr.no-ip.info")
            time.sleep(.5)


        print("Connecting...")
        self.sock.connect((server, 8000))
        print("Connected")
        time.sleep(.4)
        self.sock.send("[py]" + nick + "\n")


        self.sock.send(nick + " connected with a python client\n")
        print("registered as " + nick)
        time.sleep(.3)


    def send(self, msg):
        self.sock.send(msg + "\n")


    def disconnect(self):
        self.sock.close()

        print("disconnected")
+5
2

stdout. , - /, stdout. , - :

  • , /.

. , - , , , . .

, , ncurses. , python Windows, - , . :

+1

, , ( ) readchar (https://github.com/magmax/python-readchar/tree/master/readchar).

readchar, ( .BACKSPACE CR - . ).

, "/033 [1A" ( ), , "/n"...

self.input_buff

, , , :

            keypress = readkey()

            if keypress == key.BACKSPACE:
                self.input_buff = self.input_buff[:-1]
                print("\033[0A%s         " % self.input_buff)
                continue

            if keypress != key.CR:
                self.input_buff = "%s%s" % (self.input_buff, keypress)
                print("\033[0A%s" %  self.input_buff)
                continue

.

, , , , ...

+1

All Articles