当前位置: 首页>>代码示例>>Python>>正文


Python PipeIOStream.read_until方法代码示例

本文整理汇总了Python中tornado.iostream.PipeIOStream.read_until方法的典型用法代码示例。如果您正苦于以下问题:Python PipeIOStream.read_until方法的具体用法?Python PipeIOStream.read_until怎么用?Python PipeIOStream.read_until使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tornado.iostream.PipeIOStream的用法示例。


在下文中一共展示了PipeIOStream.read_until方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Shell

# 需要导入模块: from tornado.iostream import PipeIOStream [as 别名]
# 或者: from tornado.iostream.PipeIOStream import read_until [as 别名]
class Shell(object):
    def __init__(self, stdin=sys.stdin, stdout=sys.stdout, context: dict={}):
        """
        Create a new shell.

        :param stdin: file handle of the stdandard input
        :param stdout: file handle of the standard output
        :param context: exposed variables
        """
        self.stdin = PipeIOStream(stdin.fileno())
        self.stdout = PipeIOStream(stdout.fileno())
        self.input_buffer = []
        self.running = False
        self.context = context

    def start(self):
        self.running = True
        self.stdout.write(b"\r$>")
        self.stdin.read_until(b'\n', self.on_line)

    def on_line(self, chunk_bytes: bytes):
        chunk = chunk_bytes.decode('utf-8', errors='ignore').rstrip('\n')
        if not chunk.endswith('\\'):
            self.input_buffer.append(chunk.strip())
            line = " ".join(self.input_buffer)
            self.input_buffer.clear()
            self.on_command(line)
        else:
            self.input_buffer.append(chunk[:-1].strip())
            self.stdout.write(b"\r  ")
        if self.running:
            self.start()

    @tornado.gen.engine
    def on_command(self, command):
        try:
            if command:
                code = compile(command + '\n', '<shell>', 'single')
                res = eval(code, self.context)
                if res is not None:
                    r = pprint.pformat(res).encode('utf-8')
                    yield tornado.gen.Task(self.stdout.write, r + b'\n')
        except SystemExit:
            raise
        except:
            yield tornado.gen.Task(self.stdout.write, traceback.format_exc().encode('utf-8'))
开发者ID:johnnydeamer,项目名称:tornado-shell,代码行数:48,代码来源:__init__.py

示例2: test_pipe_iostream

# 需要导入模块: from tornado.iostream import PipeIOStream [as 别名]
# 或者: from tornado.iostream.PipeIOStream import read_until [as 别名]
    def test_pipe_iostream(self):
        r, w = os.pipe()

        rs = PipeIOStream(r, io_loop=self.io_loop)
        ws = PipeIOStream(w, io_loop=self.io_loop)

        ws.write(b"hel")
        ws.write(b"lo world")

        rs.read_until(b" ", callback=self.stop)
        data = self.wait()
        self.assertEqual(data, b"hello ")

        rs.read_bytes(3, self.stop)
        data = self.wait()
        self.assertEqual(data, b"wor")

        ws.close()

        rs.read_until_close(self.stop)
        data = self.wait()
        self.assertEqual(data, b"ld")

        rs.close()
开发者ID:tomjpsun,项目名称:Blend4Web,代码行数:26,代码来源:iostream_test.py


注:本文中的tornado.iostream.PipeIOStream.read_until方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。