【发布时间】:2021-10-27 19:40:21
【问题描述】:
我正在使用 Python 制作一个简单的网络服务器,并且我希望能够在显示服务器获取的请求时运行命令。通常会发生这种情况:
Request from 127.0.0.1 - GET /index.html
Request from 127.0.0.1 - GET /login.html
Input command: asRequest from 127.0.0.1 - GET /login.htmldf
(在这个例子中,我输入as,然后请求进来,然后我输入df结束)
我想将提示向下移动一行,这样我就可以保留回滚缓冲区,同时保留已经输入的内容。我知道这是可能的,因为(例如)在 Minecraft 服务器控制台中,这是完成的:
(我之前开始打字)
提示、文本和光标向下移动。我尝试使用ANSI转义码来达到效果,但似乎不起作用:
import threading
from time import sleep
def print_req():
sleep(3)
# \033[_D moves the cursor _ columns to the left, 1000 just means it will be on the far left
print("\033[1000D\r\n") # \r\n to (try to) move everything down a line
t = threading.Thread(target=print_req)
t.start()
print("Request from 127.0.0.1 - GET /index.html")
input("> ") # to test, type something in the 3 seconds
发生这种情况:
如您所见,光标仅向下移动了 2 行而没有移动文本。
我已经在这里问过一个与此类似的问题,但它被设置为另一个不同问题的副本,并且没有得到回答。任何帮助将不胜感激,TIA!
【问题讨论】:
标签: python input text ansi-escape