2

ユーザーがサーバー要求に変換されるコマンドを入力している間に、サーバーからメッセージを受け取り、それらを標準出力に出力する単純なコマンド ライン クライアントを実装しようとしています。今のところそれはツイストと 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 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()

サーバーは非請求データをクライアントに送信する場合があるため、これは非同期で行う必要があります。しかし、この方法で stdout に書き込まれたテキストはMyClient.dataReceived、Cmd のコンソール処理と衝突し、ほとんど使用できなくなります。

Urwid や ncurses などの何らかのコンソール UI ライブラリを使用して、MyCmd と MyClient からの出力を画面の別の部分に配置することを考えています。

2009 年のこのスレッドは、Urwid では不可能かもしれないことを示唆しています: http://lists.excess.org/pipermail/urwid/2009-July/000715.html

そして、ncurses でそれを行うこの要点は、私にとってはうまく機能しません: https://gist.github.com/interstar/3005137

それは何とかまだ可能ですか?

4

0 に答える 0