<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>termios — POSIX style tty control — Python 3.7.4 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/language_data.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.7.4 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="tty — Terminal control functions" href="tty.html" /> <link rel="prev" title="crypt — Function to check Unix passwords" href="crypt.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/3/library/termios.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> <script type="text/javascript" src="../_static/switchers.js"></script> <style> @media only screen { table.full-width-table { width: 100%; } } </style> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="tty.html" title="tty — Terminal control functions" accesskey="N">next</a> |</li> <li class="right" > <a href="crypt.html" title="crypt — Function to check Unix passwords" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <span class="language_switcher_placeholder">en</span> <span class="version_switcher_placeholder">3.7.4</span> <a href="../index.html">Documentation </a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="unix.html" accesskey="U">Unix Specific Services</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="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 style tty control<a class="headerlink" href="#module-termios" title="Permalink to this headline">¶</a></h1> <hr class="docutils" id="index-0" /> <p>This module provides an interface to the POSIX calls for tty I/O control. For a complete description of these calls, see <em class="manpage">termios(3)</em> Unix manual page. It is only available for those Unix versions that support POSIX <em>termios</em> style tty I/O control configured during installation.</p> <p>All functions in this module take a file descriptor <em>fd</em> as their first argument. This can be an integer file descriptor, such as returned by <code class="docutils literal notranslate"><span class="pre">sys.stdin.fileno()</span></code>, or a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>, such as <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code> itself.</p> <p>This module also defines all the constants needed to work with the functions provided here; these have the same name as their counterparts in C. Please refer to your system documentation for more information on using these terminal control interfaces.</p> <p>The module defines the following functions:</p> <dl class="function"> <dt id="termios.tcgetattr"> <code class="descclassname">termios.</code><code class="descname">tcgetattr</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcgetattr" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list containing the tty attributes for file descriptor <em>fd</em>, as follows: <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> where <em>cc</em> is a list of the tty special characters (each a string of length 1, except the items with indices <code class="xref py py-const docutils literal notranslate"><span class="pre">VMIN</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">VTIME</span></code>, which are integers when these fields are defined). The interpretation of the flags and the speeds as well as the indexing in the <em>cc</em> array must be done using the symbolic constants defined in the <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> module.</p> </dd></dl> <dl class="function"> <dt id="termios.tcsetattr"> <code class="descclassname">termios.</code><code class="descname">tcsetattr</code><span class="sig-paren">(</span><em>fd</em>, <em>when</em>, <em>attributes</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcsetattr" title="Permalink to this definition">¶</a></dt> <dd><p>Set the tty attributes for file descriptor <em>fd</em> from the <em>attributes</em>, which is a list like the one returned by <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>. The <em>when</em> argument determines when the attributes are changed: <code class="xref py py-const docutils literal notranslate"><span class="pre">TCSANOW</span></code> to change immediately, <code class="xref py py-const docutils literal notranslate"><span class="pre">TCSADRAIN</span></code> to change after transmitting all queued output, or <code class="xref py py-const docutils literal notranslate"><span class="pre">TCSAFLUSH</span></code> to change after transmitting all queued output and discarding all queued input.</p> </dd></dl> <dl class="function"> <dt id="termios.tcsendbreak"> <code class="descclassname">termios.</code><code class="descname">tcsendbreak</code><span class="sig-paren">(</span><em>fd</em>, <em>duration</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcsendbreak" title="Permalink to this definition">¶</a></dt> <dd><p>Send a break on file descriptor <em>fd</em>. A zero <em>duration</em> sends a break for 0.25–0.5 seconds; a nonzero <em>duration</em> has a system dependent meaning.</p> </dd></dl> <dl class="function"> <dt id="termios.tcdrain"> <code class="descclassname">termios.</code><code class="descname">tcdrain</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcdrain" title="Permalink to this definition">¶</a></dt> <dd><p>Wait until all output written to file descriptor <em>fd</em> has been transmitted.</p> </dd></dl> <dl class="function"> <dt id="termios.tcflush"> <code class="descclassname">termios.</code><code class="descname">tcflush</code><span class="sig-paren">(</span><em>fd</em>, <em>queue</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcflush" title="Permalink to this definition">¶</a></dt> <dd><p>Discard queued data on file descriptor <em>fd</em>. The <em>queue</em> selector specifies which queue: <code class="xref py py-const docutils literal notranslate"><span class="pre">TCIFLUSH</span></code> for the input queue, <code class="xref py py-const docutils literal notranslate"><span class="pre">TCOFLUSH</span></code> for the output queue, or <code class="xref py py-const docutils literal notranslate"><span class="pre">TCIOFLUSH</span></code> for both queues.</p> </dd></dl> <dl class="function"> <dt id="termios.tcflow"> <code class="descclassname">termios.</code><code class="descname">tcflow</code><span class="sig-paren">(</span><em>fd</em>, <em>action</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcflow" title="Permalink to this definition">¶</a></dt> <dd><p>Suspend or resume input or output on file descriptor <em>fd</em>. The <em>action</em> argument can be <code class="xref py py-const docutils literal notranslate"><span class="pre">TCOOFF</span></code> to suspend output, <code class="xref py py-const docutils literal notranslate"><span class="pre">TCOON</span></code> to restart output, <code class="xref py py-const docutils literal notranslate"><span class="pre">TCIOFF</span></code> to suspend input, or <code class="xref py py-const docutils literal notranslate"><span class="pre">TCION</span></code> to restart input.</p> </dd></dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>Module <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>Convenience functions for common terminal control operations.</p> </dd> </dl> </div> <div class="section" id="example"> <span id="termios-example"></span><h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2> <p>Here’s a function that prompts for a password with echoing turned off. Note the technique using a separate <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> call and 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> statement to ensure that the old tty attributes are restored exactly no matter what happens:</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> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table of Contents</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 style tty control</a><ul> <li><a class="reference internal" href="#example">Example</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="crypt.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> — Function to check Unix passwords</a></p> <h4>Next topic</h4> <p class="topless"><a href="tty.html" title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tty</span></code> — Terminal control functions</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.7/Doc/library/termios.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="tty.html" title="tty — Terminal control functions" >next</a> |</li> <li class="right" > <a href="crypt.html" title="crypt — Function to check Unix passwords" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <span class="language_switcher_placeholder">en</span> <span class="version_switcher_placeholder">3.7.4</span> <a href="../index.html">Documentation </a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="unix.html" >Unix Specific Services</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 13, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1. </div> </body> </html>