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


Python Telnet.fileno方法代码示例

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


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

示例1: __init__

# 需要导入模块: from telnetlib import Telnet [as 别名]
# 或者: from telnetlib.Telnet import fileno [as 别名]
class MPDSource:
    def __init__(self, host="localhost", port=6600):
        self._host, self._port = host, port
        self._conn = None
        self._last_elapsed = 0
        self._last_tottime = 0
        self._last_time = time()
        self._is_playing = False

    def _wait(self):
        try:
            self._conn.write(b"idle\n")
        except IOError:
            self.disconnect()
            pass

    def _make_dict(self, blob):
        result = {}
        for line in (blob or "").splitlines():
            if ":" in line:
                key, value = [s.strip() for s in line.split(":", maxsplit=1)]
                result[key.lower()] = value
        return result

    def _read_response(self):
        try:
            return self._conn.read_until(b"OK\n")
        except EOFError:
            self.disconnect()
            return b""

    def connect(self):
        try:
            self._conn = Telnet(host=self._host, port=self._port)
        except socket.error as err:
            pass  # We do not want to print it...
        else:
            # Read the OK MPD 0.17.0 line
            self._conn.read_until(b"\n")

            # Put ourself to event-looping mode
            self._wait()

    def is_connected(self):
        return not self.fileno() is -1

    def disconnect(self):
        if self._conn:
            self._conn.close()
        self._conn = None

    def _process_info(self, info):
        self._is_playing = unstopped = info["state"] in ["play", "pause"]
        if unstopped:
            markup = "<i> {title}<small> by </small>{artist}<small> on </small>{album} </i>".format(
                title=GLib.markup_escape_text(info.get("title", "n/a")),
                artist=GLib.markup_escape_text(info.get("artist", "n/a")),
                album=GLib.markup_escape_text(info.get("album", "n/a")),
            )
            self._last_elapsed = float(info.get("elapsed", 0))

            _, total_time = info.get("time", "0:1").split(":", 1)
            self._last_tottime = float(total_time)

            if self._last_tottime:
                percent = self._last_elapsed / self._last_tottime
            else:
                percent = 0
            self._last_time = time()
        else:
            percent = 0
            markup = "<i> (( not playing )) </i>"

        return {"music_markup": repr(markup), "music_percent": percent, "music_unstopped": unstopped}

    def _guess_elapsed_from_time(self):
        if self._is_playing:
            diff = time() - self._last_time
            if self._last_tottime:
                return {"music_percent": (self._last_elapsed + diff) / self._last_tottime}
            else:
                return {"music_percent": 0}
        else:
            return {}

    def read(self, has_input):
        info = {}
        if has_input:
            events = self._read_response()
            if b"player" in events:
                self._conn.write(b"command_list_begin\nstatus\ncurrentsong\ncommand_list_end\n")
                blob = self._read_response()
                raw_info = self._make_dict(blob.decode("utf-8"))
                info = self._process_info(raw_info)

            if self.is_connected():
                self._wait()
        else:
            info = self._guess_elapsed_from_time()
        return info
#.........这里部分代码省略.........
开发者ID:Estman,项目名称:par.py,代码行数:103,代码来源:par_writer.py

示例2: MPDSource

# 需要导入模块: from telnetlib import Telnet [as 别名]
# 或者: from telnetlib.Telnet import fileno [as 别名]
class MPDSource():
    def __init__(self, host='localhost', port=6600):
        self._host, self._port = host, port
        self._conn = None
        self._last_elapsed = 0
        self._last_tottime = 0
        self._last_time = time()
        self._is_playing = False

    def _wait(self):
        try:
            self._conn.write(b'idle\n')
        except IOError:
            self.disconnect()
            pass

    def _make_dict(self, blob):
        result = {}
        for line in (blob or '').splitlines():
            if ':' in line:
                key, value = [s.strip() for s in line.split(':', maxsplit=1)]
                result[key] = value
        return result

    def _read_response(self):
        try:
            return self._conn.read_until(b'OK\n')
        except EOFError:
            self.disconnect()
            return b''

    def connect(self):
        try:
            self._conn = Telnet(host=self._host, port=self._port)
        except socket.error as err:
            # print(err)  # We do not want to print it...
            pass
        else:
            # Read the OK MPD 0.17.0 line
            self._conn.read_until(b'\n')

            # Put ourself to event-looping mode
            self._wait()

    def is_connected(self):
        return not self.fileno() is -1

    def disconnect(self):
        if self._conn:
            self._conn.close()
        self._conn = None

    def _process_info(self, info):
        self._is_playing = unstopped = info['state'] in ['play', 'pause']
        if unstopped:
            markup = '<i> {title}<small> by </small>{artist}<small> on </small>{album} </i>'.format(
                title=GLib.markup_escape_text(info.get('Title', 'n/a')),
                artist=GLib.markup_escape_text(info.get('Artist', 'n/a')),
                album=GLib.markup_escape_text(info.get('Album', 'n/a'))
            )
            self._last_elapsed = float(info['elapsed'])
            self._last_tottime = float(info['Time'])
            percent = self._last_elapsed / self._last_tottime
            self._last_time = time()
        else:
            markup = '<i> (( not playing )) </i>'

        return {
                'music_markup': repr(markup),
                'music_percent': percent,
                'music_unstopped': unstopped
        }

    def _guess_elapsed_from_time(self):
        if self._is_playing:
            diff = time() - self._last_time
            return {
                'music_percent': (self._last_elapsed + diff) / self._last_tottime
            }
        else:
            return {}

    def read(self, has_input):
        info = {}
        if has_input:
            events = self._read_response()
            if b'player' in events:
                self._conn.write(b'command_list_begin\nstatus\ncurrentsong\ncommand_list_end\n')
                blob = self._read_response()
                raw_info = self._make_dict(blob.decode('utf-8'))
                info = self._process_info(raw_info)

            if self.is_connected():
                self._wait()
        else:
            info = self._guess_elapsed_from_time()
        return info

    def fileno(self):
        return  self._conn.fileno() if self._conn else -1
开发者ID:marulkan,项目名称:par.py,代码行数:102,代码来源:par_writer.py


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