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


Python BufferedReader.readline方法代码示例

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


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

示例1: read

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import readline [as 别名]
def read(rfile: io.BufferedReader) -> typing.Any:
    x = rfile.readline().strip()
    return json.loads(x)
开发者ID:mitmproxy,项目名称:mitmproxy,代码行数:5,代码来源:windows.py

示例2: _wait_for_line_in_stream

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import readline [as 别名]
def _wait_for_line_in_stream(stream: io.BufferedReader, timeout: float) -> str:
    """Wait for a line to appear in a stream and return it.

    This will only work on Unix.
    If something does appear in the stream, but it isn't terminated with a newline, then this
    function will hang. But since the program that we will use this for will write its output in
    lines, I don't think that the additional robustness is needed.
    """
    line_selector = select.poll()
    line_selector.register(stream, select.POLLIN)

    start_time = time.perf_counter()
    while time.perf_counter() - start_time < timeout:
        if line_selector.poll(0.01):
            return stream.readline()
    pytest.fail('Waiting for a stream line timed out.')
开发者ID:butla,项目名称:experiments,代码行数:18,代码来源:test_functional.py

示例3: read

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import readline [as 别名]
    def read(self, file_path):
        while not self.writing:
            time.sleep(1)

        print "Read starting..."

        f = ASIO.open(file_path, opener=False)
        s = BufferedReader(f)

        orig_path = f.get_path()
        stale_since = None

        while True:
            if f is None:
                print 'Opening file...'
                f = ASIO.open(file_path, opener=False)
                s = BufferedReader(f)

            # Try read line
            line = s.readline()

            if line:
                stale_since = None
                time.sleep(0.05)
            else:
                if stale_since is None:
                    stale_since = time.time()
                    time.sleep(0.1)
                    continue
                elif (time.time() - stale_since) > 2 and f.get_path() != orig_path:
                    s.close()
                    s = None

                    f.close()
                    f = None
                elif not self.writing:
                    break
                else:
                    time.sleep(0.1)
                    continue

            print 'read %r' % (line,)

        print 'finished'
        s.close()
        f.close()
开发者ID:fuzeman,项目名称:ASIO,代码行数:48,代码来源:test_log_rotation.py

示例4: MultipartReader

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import readline [as 别名]
class MultipartReader(object):
    """
    Reads a stream formatted as multipart/form-data.  Provides each part as a
    readable interface, avoiding loading the entire body into memory or
    caching to a file.

    Usage:

    reader = MultipartReader(f, boundary)
    part1 = reader.next_part()
    print part1.form_name()
    data = part1.read(1024)
    """

    def __init__(self, stream, boundary=None):
        b = "\r\n--" + boundary + "--"
        stream = _StreamWrapper(stream)

        self.buf_reader = BufferedReader(stream)
        self.nl = b[:2]
        self.nl_dash_boundary = b[:len(b)-2]
        self.dash_boundary_dash = b[2:]
        self.dash_boundary = b[2:len(b)-2]
        self.headers = {}
        self.parts_read = 0

        self.current_part = None

    def iter_parts(self):
        """
        Returns an iterator over the Parts in multipart/form-data.

        Do not use if you're skipping the end of the data, as the last
        iteration will seek to the end of the stream.
        """
        part = self.next_part()
        while part != None:
            yield part
            part = self.next_part()

    def next_part(self):
        """
        Returns the next Part in the stream.  If a previous part was not read
        completely, it will seek to the beginning of the next part, closing the
        previous one.
        """

        if self.current_part != None:
            self.current_part.close()

        expect_new_part = False
        while True:
            line = self.buf_reader.readline()
            is_EOF = self.buf_reader.peek(1)
            if len(is_EOF) == 0 and self.is_final_boundary(line):
                return None

            if self.is_boundary_delimeter_line(line):
                #print "Creating new part"
                self.parts_read += 1
                bp = _new_part(self)
                self.current_part = bp
                return bp

            if self.is_final_boundary(line):
                return None

            if expect_new_part:
                raise Exception("expecting a new Part, got line %s" % line)

            if self.parts_read == 0:
                continue

            if line == self.nl:
                expect_new_part = True
                continue

            raise Exception("Unexpected line in next_part(): %s" % line)

    def is_final_boundary(self, line):
        if not line.startswith(self.dash_boundary_dash):
            return False
        rest = line[len(self.dash_boundary_dash):]
        rest = skipLWSPChar(rest)
        return len(rest) == 0 or rest == self.nl

    def is_boundary_delimeter_line(self, line):
        if not line.startswith(self.dash_boundary):
            return False

        rest = line[len(self.dash_boundary):]
        rest = skipLWSPChar(rest)
        if self.parts_read == 0 and len(rest) == 1 and rest[0] == "\n":
            self.nl = self.nl[1:]
            self.nl_dash_boundary = self.nl_dash_boundary[1:]
        return rest == self.nl

    def peek_buffer_is_empty_part(self, peek):
        if peek.startswith(self.dash_boundary_dash):
            rest = peek[len(self.dash_boundary_dash):]
#.........这里部分代码省略.........
开发者ID:rckclmbr,项目名称:streaming_multipart,代码行数:103,代码来源:__init__.py

示例5: Logging

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import readline [as 别名]
class Logging(Source):
    name = 'logging'
    events = [
        'logging.playing',
        'logging.action.played',
        'logging.action.unplayed'
    ]

    parsers = []

    path = None
    path_hints = PATH_HINTS

    def __init__(self, activity):
        super(Logging, self).__init__()

        self.parsers = [p(self) for p in Logging.parsers]

        self.file = None
        self.reader = None

        self.path = None

        # Pipe events to the main activity instance
        self.pipe(self.events, activity)

    def run(self):
        line = self.read_line_retry(ping=True, stale_sleep=0.5)
        if not line:
            log.info('Unable to read log file')
            return

        log.debug('Ready')

        while True:
            # Grab the next line of the log
            line = self.read_line_retry(ping=True)

            if line:
                self.process(line)
            else:
                log.info('Unable to read log file')

    def process(self, line):
        for parser in self.parsers:
            if parser.process(line):
                return True

        return False

    def read_line(self):
        if not self.file:
            path = self.get_path()
            if not path:
                raise Exception('Unable to find the location of "Plex Media Server.log"')

            # Open file
            self.file = ASIO.open(path, opener=False)
            self.file.seek(self.file.get_size(), SEEK_ORIGIN_CURRENT)

            # Create buffered reader
            self.reader = BufferedReader(self.file)

            self.path = self.file.get_path()
            log.info('Opened file path: "%s"' % self.path)

        return self.reader.readline()

    def read_line_retry(self, timeout=60, ping=False, stale_sleep=1.0):
        line = None
        stale_since = None

        while not line:
            line = self.read_line()

            if line:
                stale_since = None
                time.sleep(0.05)
                break

            if stale_since is None:
                stale_since = time.time()
                time.sleep(stale_sleep)
                continue
            elif (time.time() - stale_since) > timeout:
                return None
            elif (time.time() - stale_since) > timeout / 2:
                # Nothing returned for 5 seconds
                if self.file.get_path() != self.path:
                    log.debug("Log file moved (probably rotated), closing")
                    self.close()
                elif ping:
                    # Ping server to see if server is still active
                    Plex.detail()
                    ping = False

            time.sleep(stale_sleep)

        return line

#.........这里部分代码省略.........
开发者ID:Danik1601,项目名称:Plex-Trakt-Scrobbler,代码行数:103,代码来源:main.py

示例6: dataReceived

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import readline [as 别名]
    def dataReceived(self, data):
        """
        Parse the NATS.io protocol from chunks of data streaming from
        the connected gnatsd.

        The server settings will be set and connect will be sent with this
        client's info upon an INFO, which should happen when the
        transport connects.

        Registered message callback functions will be called with MSGs
        once parsed.

        PONG will be called upon a ping.

        An exception will be raised upon an ERR from gnatsd.

        An +OK doesn't do anything.
        """
        if self.remaining_bytes:
            data = self.remaining_bytes + data
            self.remaining_bytes = b""

        data_buf = BufferedReader(BytesIO(data))
        while True:
            command = data_buf.read(4)
            if command == b"-ERR":
                raise NatsError(data_buf.read())
            elif command == b"+OK\r":
                val = data_buf.read(1)
                if val != b"\n":
                    self.remaining_bytes += command
                    break
            elif command == b"MSG ":
                val = data_buf.readline()
                if not val:
                    self.remaining_bytes += command
                    break
                if not val.endswith(b"\r\n"):
                    self.remaining_bytes += command + val
                    break

                meta_data = val.split(b" ")
                n_bytes = int(meta_data[-1])
                subject = meta_data[0].decode()
                if len(meta_data) == 4:
                    reply_to = meta_data[2].decode()
                elif len(meta_data) == 3:
                    reply_to = None
                else:
                    self.remaining_bytes += command + val
                    break

                sid = meta_data[1].decode()

                if sid in self.sids:
                    on_msg = self.sids[sid]
                else:
                    on_msg = self.on_msg

                payload = data_buf.read(n_bytes)
                if len(payload) != n_bytes:
                    self.remaining_bytes += command + val + payload
                    break

                if on_msg:
                    on_msg(nats_protocol=self, sid=sid, subject=subject, reply_to=reply_to, payload=payload)
                else:
                    stdout.write(command.decode())
                    stdout.write(val.decode())
                    stdout.write(payload.decode())

                payload_post = data_buf.readline()
                if payload_post != b"\r\n":
                    self.remaining_bytes += command + val + payload + payload_post
                    break
            elif command == b"PING":
                self.log.info("got PING")
                self.pong()
                val = data_buf.readline()
                if val != b"\r\n":
                    self.remaining_bytes += command + val
                    break
            elif command == b"PONG":
                self.pout -= 1
                val = data_buf.readline()
                if val != b"\r\n":
                    self.remaining_bytes += command + val
                    break
            elif command == b"INFO":
                val = data_buf.readline()
                if not val.endswith(b"\r\n"):
                    self.remaining_bytes += command + val
                    break
                settings = json.loads(val.decode("utf8"))
                self.server_settings = ServerInfo(**settings)
                self.log.info("{server_info}", server_info=settings)
                self.status = CONNECTED
                self.connect()
                if self.on_connect_d:
                    self.on_connect_d.callback(self)
#.........这里部分代码省略.........
开发者ID:johnwlockwood,项目名称:txnats,代码行数:103,代码来源:txnats.py


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