将控制台输入线保持在输出下方 [英] Keep console input line below output

查看:79
本文介绍了将控制台输入线保持在输出下方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我目前正在尝试制作一个小型的TCP聊天应用程序。发送和接收消息已经可以正常工作了……但是问题是:

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

当我在收到消息的同时开始键入消息时……它出现在文本之后m写作

When i start typing a message while i receive one... it appears after the text I'm writing

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

[用户发送>你好,然后我开始写我正在写...,然后用户在发送消息之前写了我发送了...,因此它已放置在我的输入之后...
我希望传入的消息始终在输入之前!

[User sent > "hello", then I started writing "i am writing..." then user wrote " i sent a..." before i sent my message... so it has been placed after my input... I want the incoming message always to be before my input !

这是我当前的代码:
Client.py

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

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")


推荐答案

您的代码会将所有内容写入stdout。每当有东西到达您的发送者/接收者线程中的任何一个时,它都会打印到stdout。问题在于,由于输出流的基本性质,您无法完成以下操作:

Your code writes everything to stdout. Whenever something arrives to either of your sender/receiver threads, it prints to stdout. The issue with that is, due to the fundamental nature of output streams, you cannot accomplish the following :



事物严格按照发生的顺序发生。一旦有东西进入,无论光标在哪里,print语句都会将数据转储到那里。您不能在不使用更高级/更强大的构造的情况下修改该行为。

Things happen strictly in the order of occurrence. The moment something comes in, wherever the cursor is, the print statement dumps that data over there. You cannot modify that behaviour without using fancier / more powerful constructs.

为了执行您想要的操作,我将使用 ncurses 。您似乎在Windows上使用的是python,因此您将不得不对如何获得等效功能进行一些研究。查看此线程: Windows的替代方法

In order to do what you want, I would use ncurses. You seem to be using python on Windows, so you're going to have to do some digging on how to get equivalent functionality. Check out this thread : Curses alternative for windows

这篇关于将控制台输入线保持在输出下方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆