336 lines
22 KiB
HTML
336 lines
22 KiB
HTML
|
|
<!DOCTYPE html>
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
|
<head>
|
|
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
|
|
|
<title>termios --- POSIX 风格的 tty 控制 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|
|
|
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
|
<script src="../_static/jquery.js"></script>
|
|
<script src="../_static/underscore.js"></script>
|
|
<script src="../_static/doctools.js"></script>
|
|
<script src="../_static/language_data.js"></script>
|
|
<script src="../_static/translations.js"></script>
|
|
|
|
<script src="../_static/sidebar.js"></script>
|
|
|
|
<link rel="search" type="application/opensearchdescription+xml"
|
|
title="在 Python 3.8.20 文档 中搜索"
|
|
href="../_static/opensearch.xml"/>
|
|
<link rel="author" title="关于这些文档" href="../about.html" />
|
|
<link rel="index" title="索引" href="../genindex.html" />
|
|
<link rel="search" title="搜索" href="../search.html" />
|
|
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
|
<link rel="next" title="tty --- 终端控制功能" href="tty.html" />
|
|
<link rel="prev" title="crypt —— 检查 Unix 口令的函数" href="crypt.html" />
|
|
<link rel="canonical" href="https://docs.python.org/3/library/termios.html" />
|
|
|
|
|
|
|
|
|
|
|
|
<style>
|
|
@media only screen {
|
|
table.full-width-table {
|
|
width: 100%;
|
|
}
|
|
}
|
|
</style>
|
|
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
|
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
|
<script type="text/javascript" src="../_static/menu.js"></script>
|
|
|
|
</head>
|
|
<body>
|
|
<div class="mobile-nav">
|
|
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
|
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
|
<label for="menuToggler" class="toggler__label">
|
|
<span></span>
|
|
</label>
|
|
<nav class="nav-content" role="navigation">
|
|
<a href="https://www.python.org/" class="nav-logo">
|
|
<img src="../_static/py.svg" alt="Logo"/>
|
|
</a>
|
|
<div class="version_switcher_placeholder"></div>
|
|
<form role="search" class="search" action="../search.html" method="get">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
|
<path fill-rule="nonzero"
|
|
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
|
|
</svg>
|
|
<input type="text" name="q" aria-label="快速搜索"/>
|
|
<input type="submit" value="转向"/>
|
|
</form>
|
|
</nav>
|
|
<div class="menu-wrapper">
|
|
<nav class="menu" role="navigation" aria-label="main navigation">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<h3><a href="../contents.html">目录</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code> --- POSIX 风格的 tty 控制</a><ul>
|
|
<li><a class="reference internal" href="#example">示例</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>上一个主题</h4>
|
|
<p class="topless"><a href="crypt.html"
|
|
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> —— 检查 Unix 口令的函数</a></p>
|
|
<h4>下一个主题</h4>
|
|
<p class="topless"><a href="tty.html"
|
|
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tty</span></code> --- 终端控制功能</a></p>
|
|
<div role="note" aria-label="source link">
|
|
<h3>本页</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">报告 Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/termios.rst"
|
|
rel="nofollow">显示源代码
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
|
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
|
你应当升级版本,并阅读
|
|
<a href="/3/library/termios.html"> Python 当前稳定版本的文档</a>.
|
|
</div>
|
|
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>导航</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="总目录"
|
|
accesskey="I">索引</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python 模块索引"
|
|
>模块</a> |</li>
|
|
<li class="right" >
|
|
<a href="tty.html" title="tty --- 终端控制功能"
|
|
accesskey="N">下一页</a> |</li>
|
|
<li class="right" >
|
|
<a href="crypt.html" title="crypt —— 检查 Unix 口令的函数"
|
|
accesskey="P">上一页</a> |</li>
|
|
|
|
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li class="switchers">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<div class="version_switcher_placeholder"></div>
|
|
</li>
|
|
<li>
|
|
|
|
</li>
|
|
<li id="cpython-language-and-version">
|
|
<a href="../index.html">3.8.20 Documentation</a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="unix.html" accesskey="U">Unix 专有服务</a> »</li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
|
<input type="submit" value="转向" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<section id="module-termios">
|
|
<span id="termios-posix-style-tty-control"></span><h1><a class="reference internal" href="#module-termios" title="termios: POSIX style tty control. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code></a> --- POSIX 风格的 tty 控制<a class="headerlink" href="#module-termios" title="永久链接至标题">¶</a></h1>
|
|
<hr class="docutils" id="index-0" />
|
|
<p>此模块提供了针对tty I/O 控制的 POSIX 调用的接口。 有关此类调用的完整描述,请参阅 <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/termios(3)">termios(3)</a></em> Unix 指南页。 它仅在当安装时配置了支持 POSIX <em>termios</em> 风格的 tty I/O 控制的 Unix 版本上可用。</p>
|
|
<p>此模块中的所有函数均接受一个文件描述符 <em>fd</em> 作为第一个参数。 这可以是一个整数形式的文件描述符,例如 <code class="docutils literal notranslate"><span class="pre">sys.stdin.fileno()</span></code> 所返回的对象,或是一个 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>,例如 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code> 本身。</p>
|
|
<p>这个模块还定义了与此处所提供的函数一起使用的所有必要的常量;这些常量与它们在 C 中的对应常量同名。 请参考你的系统文档了解有关如何使用这些终端控制接口的更多信息。</p>
|
|
<p>这个模块定义了以下函数:</p>
|
|
<dl class="function">
|
|
<dt id="termios.tcgetattr">
|
|
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcgetattr</code><span class="sig-paren">(</span><em class="sig-param">fd</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcgetattr" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>对于文件描述符 <em>fd</em> 返回一个包含 tty 属性的列表,形式如下: <code class="docutils literal notranslate"><span class="pre">[iflag,</span> <span class="pre">oflag,</span> <span class="pre">cflag,</span> <span class="pre">lflag,</span> <span class="pre">ispeed,</span> <span class="pre">ospeed,</span> <span class="pre">cc]</span></code>,其中 <em>cc</em> 为一个包含 tty 特殊字符的列表(每一项都是长度为 1 的字符串,索引号为 <code class="xref py py-const docutils literal notranslate"><span class="pre">VMIN</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">VTIME</span></code> 的项除外,这些字段如有定义则应为整数)。 对旗标和速度以及 <em>cc</em> 数组中索引的解读必须使用在 <a class="reference internal" href="#module-termios" title="termios: POSIX style tty control. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code></a> 模块中定义的符号常量来完成。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="termios.tcsetattr">
|
|
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcsetattr</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">when</em>, <em class="sig-param">attributes</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcsetattr" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>根据 <em>attributes</em> 列表设置文件描述符 <em>fd</em> 的 tty 属性,该列表即 <a class="reference internal" href="#termios.tcgetattr" title="termios.tcgetattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">tcgetattr()</span></code></a> 所返回的对象。 <em>when</em> 参数确定何时改变属性: <code class="xref py py-const docutils literal notranslate"><span class="pre">TCSANOW</span></code> 表示立即改变,<code class="xref py py-const docutils literal notranslate"><span class="pre">TCSADRAIN</span></code> 表示在传输所有队列输出后再改变,或 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCSAFLUSH</span></code> 表示在传输所有队列输出并丢失所有队列输入后再改变。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="termios.tcsendbreak">
|
|
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcsendbreak</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">duration</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcsendbreak" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>在文件描述符 <em>fd</em> 上发送一个中断。 <em>duration</em> 为零表示发送时长为 0.25--0.5 秒的中断;<em>duration</em> 非零值的含义取决于具体系统。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="termios.tcdrain">
|
|
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcdrain</code><span class="sig-paren">(</span><em class="sig-param">fd</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcdrain" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>进入等待状态直到写入文件描述符 <em>fd</em> 的所有输出都传送完毕。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="termios.tcflush">
|
|
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcflush</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">queue</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcflush" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>在文件描述符 <em>fd</em> 上丢弃队列数据。 <em>queue</em> 选择器指定哪个队列: <code class="xref py py-const docutils literal notranslate"><span class="pre">TCIFLUSH</span></code> 表示输入队列,<code class="xref py py-const docutils literal notranslate"><span class="pre">TCOFLUSH</span></code> 表示输出队列,或 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCIOFLUSH</span></code> 表示两个队列同时。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="termios.tcflow">
|
|
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcflow</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">action</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcflow" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>在文件描述符 <em>fd</em> 上挂起一战恢复输入或输出。 <em>action</em> 参数可以为 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCOOFF</span></code> 表示挂起输出,<code class="xref py py-const docutils literal notranslate"><span class="pre">TCOON</span></code> 表示重启输出,<code class="xref py py-const docutils literal notranslate"><span class="pre">TCIOFF</span></code> 表示挂起输入,或 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCION</span></code> 表示重启输入。</p>
|
|
</dd></dl>
|
|
|
|
<div class="admonition seealso">
|
|
<p class="admonition-title">参见</p>
|
|
<dl class="simple">
|
|
<dt>模块 <a class="reference internal" href="tty.html#module-tty" title="tty: Utility functions that perform common terminal control operations. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tty</span></code></a></dt><dd><p>针对常用终端控制操作的便捷函数。</p>
|
|
</dd>
|
|
</dl>
|
|
</div>
|
|
<section id="example">
|
|
<span id="termios-example"></span><h2>示例<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h2>
|
|
<p>这个函数可提示输入密码并且关闭回显。 请注意其采取的技巧是使用一个单独的 <a class="reference internal" href="#termios.tcgetattr" title="termios.tcgetattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">tcgetattr()</span></code></a> 调用和一个 <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> ... <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 语句来确保旧的 tty 属性无论在何种情况下都会被原样保存:</p>
|
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">getpass</span><span class="p">(</span><span class="n">prompt</span><span class="o">=</span><span class="s2">"Password: "</span><span class="p">):</span>
|
|
<span class="kn">import</span> <span class="nn">termios</span><span class="o">,</span> <span class="nn">sys</span>
|
|
<span class="n">fd</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">fileno</span><span class="p">()</span>
|
|
<span class="n">old</span> <span class="o">=</span> <span class="n">termios</span><span class="o">.</span><span class="n">tcgetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
|
|
<span class="n">new</span> <span class="o">=</span> <span class="n">termios</span><span class="o">.</span><span class="n">tcgetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
|
|
<span class="n">new</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">new</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&</span> <span class="o">~</span><span class="n">termios</span><span class="o">.</span><span class="n">ECHO</span> <span class="c1"># lflags</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">termios</span><span class="o">.</span><span class="n">tcsetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TCSADRAIN</span><span class="p">,</span> <span class="n">new</span><span class="p">)</span>
|
|
<span class="n">passwd</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="n">prompt</span><span class="p">)</span>
|
|
<span class="k">finally</span><span class="p">:</span>
|
|
<span class="n">termios</span><span class="o">.</span><span class="n">tcsetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TCSADRAIN</span><span class="p">,</span> <span class="n">old</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">passwd</span>
|
|
</pre></div>
|
|
</div>
|
|
</section>
|
|
</section>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h3><a href="../contents.html">目录</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code> --- POSIX 风格的 tty 控制</a><ul>
|
|
<li><a class="reference internal" href="#example">示例</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>上一个主题</h4>
|
|
<p class="topless"><a href="crypt.html"
|
|
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> —— 检查 Unix 口令的函数</a></p>
|
|
<h4>下一个主题</h4>
|
|
<p class="topless"><a href="tty.html"
|
|
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tty</span></code> --- 终端控制功能</a></p>
|
|
<div role="note" aria-label="source link">
|
|
<h3>本页</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">报告 Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/termios.rst"
|
|
rel="nofollow">显示源代码
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>导航</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="总目录"
|
|
>索引</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python 模块索引"
|
|
>模块</a> |</li>
|
|
<li class="right" >
|
|
<a href="tty.html" title="tty --- 终端控制功能"
|
|
>下一页</a> |</li>
|
|
<li class="right" >
|
|
<a href="crypt.html" title="crypt —— 检查 Unix 口令的函数"
|
|
>上一页</a> |</li>
|
|
|
|
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li class="switchers">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<div class="version_switcher_placeholder"></div>
|
|
</li>
|
|
<li>
|
|
|
|
</li>
|
|
<li id="cpython-language-and-version">
|
|
<a href="../index.html">3.8.20 Documentation</a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="unix.html" >Unix 专有服务</a> »</li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
|
<input type="submit" value="转向" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
|
<br />
|
|
This page is licensed under the Python Software Foundation License Version 2.
|
|
<br />
|
|
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
|
<br />
|
|
|
|
<br />
|
|
|
|
The Python Software Foundation is a non-profit corporation.
|
|
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
|
<br />
|
|
<br />
|
|
|
|
最后更新于 12月 09, 2024.
|
|
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
|
<br />
|
|
|
|
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
|
</div>
|
|
|
|
</body>
|
|
</html> |