Python 在另一个线程中同时打印内容时使用Cmd
我试图实现一个简单的命令行客户机,它接受来自服务器的消息并将它们打印到标准输出,同时用户输入转换为服务器请求的命令。现在使用twisted和Cmd完成:Python 在另一个线程中同时打印内容时使用Cmd,python,cmd,twisted,python-curses,urwid,Python,Cmd,Twisted,Python Curses,Urwid,我试图实现一个简单的命令行客户机,它接受来自服务器的消息并将它们打印到标准输出,同时用户输入转换为服务器请求的命令。现在使用twisted和Cmd完成: from sys import stdout from cmd import Cmd from twisted.internet import reactor from twisted.internet.protocol import Protocol from twisted.internet.endpoints import TCP4Cl
from sys import stdout
from cmd import Cmd
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
class MyCmd(Cmd):
def __init__(self, client):
Cmd.__init__(self)
self.client = client
def default(self, line):
client.sendData(line)
def do_EOF(self, line):
if reactor.running:
reactor.stop()
stdout.write("\n")
return True
class MyClient(Protocol):
def dataReceived(self, data):
stdout.write(data)
def sendData(self, data):
self.transport.write(data)
self.transport.write("\n")
point = TCP4ClientEndpoint(reactor, "localhost", 6004)
client = MyClient()
connectProtocol(point, client)
reactor.callInThread(MyCmd(client).cmdloop)
reactor.run()
这需要异步完成,因为服务器有时只是向客户端发送未经请求的数据。
但是通过这种方式,MyClient.dataReceived
写入stdout的文本与Cmd的控制台处理冲突,几乎无法使用
我正在考虑使用某种控制台UI库,如Urwid或ncurses,将MyCmd和MyClient的输出放在屏幕的不同部分
2009年的这条线索表明,使用Urwid可能是不可能的:
而这一与ncurses相关的要点对我来说并不太合适:
不知何故,这仍然是可能的吗