python - 如何制作具有 readline 功能的扭曲 python 客户端

标签 python twisted readline

我正在尝试使用 Python Twisted 为简单的 TCP 服务器编写客户端。当然,我是 Python 的新手,刚开始研究 Twisted,所以我可能会做错一切。

服务器很简单,您打算使用 nc 或 telnet。没有身份验证。您只需连接并获得一个简单的控制台。我想编写一个添加一些 readline 功能的客户端(历史和 emacs,如 ctrl-a/ctrl-e 是我所追求的)

下面是我编写的代码,其效果与从命令行使用 netcat 一样好,例如 nc localhost 4118

from twisted.internet import reactor, protocol, stdio
from twisted.protocols import basic
from sys import stdout

host='localhost'
port=4118
console_delimiter='\n'

class MyConsoleClient(protocol.Protocol):
    def dataReceived(self, data):
        stdout.write(data)
        stdout.flush()

    def sendData(self,data):
        self.transport.write(data+console_delimiter)

class MyConsoleClientFactory(protocol.ClientFactory):
    def startedConnecting(self,connector):
        print 'Starting connection to console.'

    def buildProtocol(self, addr):
        print 'Connected to console!'
        self.client = MyConsoleClient()
        self.client.name = 'console'
        return self.client

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed with reason:', reason

class Console(basic.LineReceiver):
    factory = None
    delimiter = console_delimiter

    def __init__(self,factory):
        self.factory = factory

    def lineReceived(self,line):
        if line == 'quit':
            self.quit()
        else:
            self.factory.client.sendData(line)

    def quit(self):
        reactor.stop()

def main():
    factory = MyConsoleClientFactory()
    stdio.StandardIO(Console(factory))
    reactor.connectTCP(host,port,factory)
    reactor.run()

if __name__ == '__main__':
    main()

输出:

$ python ./console-console-client.py 
Starting connection to console.
Connected to console!
console> version
d305dfcd8fc23dc6674a1d18567a3b4e8383d70e
console> number-events
338
console> quit

我看过

Python Twisted integration with Cmd module

这对我来说真的行不通。示例代码运行良好,但当我引入网络时,我似乎遇到了与 stdio 的竞争条件。这个较旧的链接似乎提倡类似的方法(在单独的线程中运行 readline),但我并没有深入了解它。

我也研究过扭曲的海螺侮辱,但除了演示示例之外,我没有任何运气让任何东西起作用。

制作提供 readline 支持的基于终端的客户端的最佳方法是什么?

http://twistedmatrix.com/documents/current/api/twisted.conch.stdio.html

看起来很有前途,但我不知道如何使用它。

http://twistedmatrix.com/documents/current/api/twisted.conch.recvline.HistoricRecvLine.html

似乎也提供了对处理向上和向下箭头的支持,但我无法将控制台切换为从 HistoricRecVLine 继承而不是 LineReceiver 来运行。

也许 twisted 是要使用的错误框架,或者我应该使用所有 conch 类。我只是喜欢它的事件驱动风格。有没有更好/更简单的方法来在扭曲的客户端中获得 readline 或类似 readline 的支持?

最佳答案

我最终通过不使用 Twisted 框架解决了这个问题。这是一个很棒的框架,但我认为它不是这项工作的错误工具。相反,我使用了 telnetlibcmdreadline 模块。

我的服务器是异步的,但这并不意味着我的客户端需要异步,所以我使用 telnetlib 与服务器进行通信。这使得创建 ConsoleClient 类成为 cmd.Cmd 的子类并获取历史记录和类似 emacs 的快捷方式变得容易。

#! /usr/bin/env python

import telnetlib
import readline
import os
import sys
import atexit
import cmd
import string

HOST='127.0.0.1'
PORT='4118'

CONSOLE_PROMPT='console> '

class ConsoleClient(cmd.Cmd):
    """Simple Console Client in Python.  This allows for readline functionality."""

    def connect_to_console(self):
        """Can throw an IOError if telnet connection fails."""
        self.console = telnetlib.Telnet(HOST,PORT)
        sys.stdout.write(self.read_from_console())
        sys.stdout.flush()

    def read_from_console(self):
        """Read from console until prompt is found (no more data to read)
        Will throw EOFError if the console is closed.
        """
        read_data = self.console.read_until(CONSOLE_PROMPT)
        return self.strip_console_prompt(read_data)

    def strip_console_prompt(self,data_received):
        """Strip out the console prompt if present"""
        if data_received.startswith(CONSOLE_PROMPT):
            return data_received.partition(CONSOLE_PROMPT)[2]
        else:
            #The banner case when you first connect
            if data_received.endswith(CONSOLE_PROMPT):
                return data_received.partition(CONSOLE_PROMPT)[0]
            else:
                return data_received

    def run_console_command(self,line):
        self.write_to_console(line + '\n')
        data_recved = self.read_from_console()        
        sys.stdout.write(self.strip_console_prompt(data_recved))        
        sys.stdout.flush()

    def write_to_console(self,line):
        """Write data to the console"""
        self.console.write(line)
        sys.stdout.flush()

    def do_EOF(self, line): 
        try:
            self.console.write("quit\n")
            self.console.close()
        except IOError:
            pass
        return True

    def do_help(self,line):
        """The server already has it's own help command.  Use that"""
        self.run_console_command("help\n")

    def do_quit(self, line):        
        return self.do_EOF(line)

    def default(self, line):
        """Allow a command to be sent to the console."""
        self.run_console_command(line)

    def emptyline(self):
        """Don't send anything to console on empty line."""
        pass


def main():
    histfile = os.path.join(os.environ['HOME'], '.consolehistory') 
    try:
        readline.read_history_file(histfile) 
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile) 

    try:
        console_client = ConsoleClient()
        console_client.prompt = CONSOLE_PROMPT
        console_client.connect_to_console()
        doQuit = False;
        while doQuit != True:
            try:
                console_client.cmdloop()
                doQuit = True;
            except KeyboardInterrupt:
                #Allow for ^C (Ctrl-c)
                sys.stdout.write('\n')
    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
    except EOFError:
        pass

if __name__ == '__main__':
    main()

我所做的一项更改是删除从服务器返回的提示并使用 Cmd.prompt 显示给用户。我的理由是支持 Ctrl-c 更像一个 shell。

关于python - 如何制作具有 readline 功能的扭曲 python 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13871738/

相关文章:

node.js - 如何使用quirer.prompt为输入添加前缀

python - C++ 嵌入 Python

python - Django ImportError 在/admin/

python - 在Python中使用twisted以异步模式发送数据

node.js - zeroMQ vs node.js vs beanstalkd vs Twisted

python - 高速公路从外部应用程序发送用户特定和广播消息

python read() from stdout 比逐行读取慢得多(吞咽?)

java - 在控制台中读取我自己打印的行

python - 安装了 python 3 和 pip 3 但 pip -V 显示 19.2.3

Python-pandas 将 NA 替换为数据框中一组的中位数或均值