parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="Transports and Protocols" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/asyncio-protocol.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Preface Transports and Protocols are used by the low-level event loop APIs such as loop.create_connection(). They use callback-based programming style and enable high-performance implementations of..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_asyncio-protocol_ba0bf6d5.png" />15<meta property="og:image:alt" content="Preface Transports and Protocols are used by the low-level event loop APIs such as loop.create_connection(). They use callback-based programming style and enable high-performance implementations of..." />16<meta name="description" content="Preface Transports and Protocols are used by the low-level event loop APIs such as loop.create_connection(). They use callback-based programming style and enable high-performance implementations of..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>Transports and Protocols — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="Policies" href="asyncio-policy.html" />43 <link rel="prev" title="Futures" href="asyncio-future.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/asyncio-protocol.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" 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"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#">Transports and Protocols</a><ul>108<li><a class="reference internal" href="#transports">Transports</a><ul>109<li><a class="reference internal" href="#transports-hierarchy">Transports Hierarchy</a></li>110<li><a class="reference internal" href="#base-transport">Base Transport</a></li>111<li><a class="reference internal" href="#read-only-transports">Read-only Transports</a></li>112<li><a class="reference internal" href="#write-only-transports">Write-only Transports</a></li>113<li><a class="reference internal" href="#datagram-transports">Datagram Transports</a></li>114<li><a class="reference internal" href="#subprocess-transports">Subprocess Transports</a></li>115</ul>116</li>117<li><a class="reference internal" href="#protocols">Protocols</a><ul>118<li><a class="reference internal" href="#base-protocols">Base Protocols</a></li>119<li><a class="reference internal" href="#base-protocol">Base Protocol</a></li>120<li><a class="reference internal" href="#streaming-protocols">Streaming Protocols</a></li>121<li><a class="reference internal" href="#buffered-streaming-protocols">Buffered Streaming Protocols</a></li>122<li><a class="reference internal" href="#datagram-protocols">Datagram Protocols</a></li>123<li><a class="reference internal" href="#subprocess-protocols">Subprocess Protocols</a></li>124</ul>125</li>126<li><a class="reference internal" href="#examples">Examples</a><ul>127<li><a class="reference internal" href="#tcp-echo-server">TCP Echo Server</a></li>128<li><a class="reference internal" href="#tcp-echo-client">TCP Echo Client</a></li>129<li><a class="reference internal" href="#udp-echo-server">UDP Echo Server</a></li>130<li><a class="reference internal" href="#udp-echo-client">UDP Echo Client</a></li>131<li><a class="reference internal" href="#connecting-existing-sockets">Connecting Existing Sockets</a></li>132<li><a class="reference internal" href="#loop-subprocess-exec-and-subprocessprotocol">loop.subprocess_exec() and SubprocessProtocol</a></li>133</ul>134</li>135</ul>136</li>137</ul>138 139 </div>140 <div>141 <h4>Previous topic</h4>142 <p class="topless"><a href="asyncio-future.html"143 title="previous chapter">Futures</a></p>144 </div>145 <div>146 <h4>Next topic</h4>147 <p class="topless"><a href="asyncio-policy.html"148 title="next chapter">Policies</a></p>149 </div>150 <script>151 document.addEventListener('DOMContentLoaded', () => {152 const title = document.querySelector('meta[property="og:title"]').content;153 const elements = document.querySelectorAll('.improvepage');154 const pageurl = window.location.href.split('?')[0];155 elements.forEach(element => {156 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));157 url.searchParams.set('pagetitle', title);158 url.searchParams.set('pageurl', pageurl);159 url.searchParams.set('pagesource', "library/asyncio-protocol.rst");160 element.href = url.toString();161 });162 });163 </script>164 <div role="note" aria-label="source link">165 <h3>This page</h3>166 <ul class="this-page-menu">167 <li><a href="../bugs.html">Report a bug</a></li>168 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>169 <li>170 <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-protocol.rst?plain=1"171 rel="nofollow">Show source172 </a>173 </li>174 175 </ul>176 </div>177 </nav>178 </div>179</div>180 181 182 <div class="related" role="navigation" aria-label="Related">183 <h3>Navigation</h3>184 <ul>185 <li class="right" style="margin-right: 10px">186 <a href="../genindex.html" title="General Index"187 accesskey="I">index</a></li>188 <li class="right" >189 <a href="../py-modindex.html" title="Python Module Index"190 >modules</a> |</li>191 <li class="right" >192 <a href="asyncio-policy.html" title="Policies"193 accesskey="N">next</a> |</li>194 <li class="right" >195 <a href="asyncio-future.html" title="Futures"196 accesskey="P">previous</a> |</li>197 198 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>199 <li><a href="https://www.python.org/">Python</a> »</li>200 <li class="switchers">201 <div class="language_switcher_placeholder"></div>202 <div class="version_switcher_placeholder"></div>203 </li>204 <li>205 206 </li>207 <li id="cpython-language-and-version">208 <a href="../index.html">3.15.0a6 Documentation</a> »209 </li>210 211 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>212 <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> »</li>213 <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> »</li>214 <li class="nav-item nav-item-this"><a href="">Transports and Protocols</a></li>215 <li class="right">216 217 218 <div class="inline-search" role="search">219 <form class="inline-search" action="../search.html" method="get">220 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">221 <input type="submit" value="Go">222 </form>223 </div>224 |225 </li>226 <li class="right">227<label class="theme-selector-label">228 Theme229 <select class="theme-selector" oninput="activateTheme(this.value)">230 <option value="auto" selected>Auto</option>231 <option value="light">Light</option>232 <option value="dark">Dark</option>233 </select>234</label> |</li>235 236 </ul>237 </div> 238 239 <div class="document">240 <div class="documentwrapper">241 <div class="bodywrapper">242 <div class="body" role="main">243 244 <section id="transports-and-protocols">245<span id="asyncio-transports-protocols"></span><h1>Transports and Protocols<a class="headerlink" href="#transports-and-protocols" title="Link to this heading">¶</a></h1>246<p class="rubric">Preface</p>247<p>Transports and Protocols are used by the <strong>low-level</strong> event loop248APIs such as <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a>. They use249callback-based programming style and enable high-performance250implementations of network or IPC protocols (e.g. HTTP).</p>251<p>Essentially, transports and protocols should only be used in252libraries and frameworks and never in high-level asyncio253applications.</p>254<p>This documentation page covers both <a class="reference internal" href="#transports">Transports</a> and <a class="reference internal" href="#protocols">Protocols</a>.</p>255<p class="rubric">Introduction</p>256<p>At the highest level, the transport is concerned with <em>how</em> bytes257are transmitted, while the protocol determines <em>which</em> bytes to258transmit (and to some extent when).</p>259<p>A different way of saying the same thing: a transport is an260abstraction for a socket (or similar I/O endpoint) while a protocol261is an abstraction for an application, from the transport’s point262of view.</p>263<p>Yet another view is the transport and protocol interfaces264together define an abstract interface for using network I/O and265interprocess I/O.</p>266<p>There is always a 1:1 relationship between transport and protocol267objects: the protocol calls transport methods to send data,268while the transport calls protocol methods to pass it data that269has been received.</p>270<p>Most of connection oriented event loop methods271(such as <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a>) usually accept a272<em>protocol_factory</em> argument used to create a <em>Protocol</em> object273for an accepted connection, represented by a <em>Transport</em> object.274Such methods usually return a tuple of <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>.</p>275<p class="rubric">Contents</p>276<p>This documentation page contains the following sections:</p>277<ul class="simple">278<li><p>The <a class="reference internal" href="#transports">Transports</a> section documents asyncio <a class="reference internal" href="#asyncio.BaseTransport" title="asyncio.BaseTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseTransport</span></code></a>,279<a class="reference internal" href="#asyncio.ReadTransport" title="asyncio.ReadTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadTransport</span></code></a>, <a class="reference internal" href="#asyncio.WriteTransport" title="asyncio.WriteTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteTransport</span></code></a>, <a class="reference internal" href="#asyncio.Transport" title="asyncio.Transport"><code class="xref py py-class docutils literal notranslate"><span class="pre">Transport</span></code></a>,280<a class="reference internal" href="#asyncio.DatagramTransport" title="asyncio.DatagramTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">DatagramTransport</span></code></a>, and <a class="reference internal" href="#asyncio.SubprocessTransport" title="asyncio.SubprocessTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">SubprocessTransport</span></code></a>281classes.</p></li>282<li><p>The <a class="reference internal" href="#protocols">Protocols</a> section documents asyncio <a class="reference internal" href="#asyncio.BaseProtocol" title="asyncio.BaseProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseProtocol</span></code></a>,283<a class="reference internal" href="#asyncio.Protocol" title="asyncio.Protocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">Protocol</span></code></a>, <a class="reference internal" href="#asyncio.BufferedProtocol" title="asyncio.BufferedProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedProtocol</span></code></a>,284<a class="reference internal" href="#asyncio.DatagramProtocol" title="asyncio.DatagramProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">DatagramProtocol</span></code></a>, and <a class="reference internal" href="#asyncio.SubprocessProtocol" title="asyncio.SubprocessProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">SubprocessProtocol</span></code></a> classes.</p></li>285<li><p>The <a class="reference internal" href="#examples">Examples</a> section showcases how to work with transports,286protocols, and low-level event loop APIs.</p></li>287</ul>288<section id="transports">289<span id="asyncio-transport"></span><h2>Transports<a class="headerlink" href="#transports" title="Link to this heading">¶</a></h2>290<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/asyncio/transports.py">Lib/asyncio/transports.py</a></p>291<hr class="docutils" />292<p>Transports are classes provided by <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> in order to abstract293various kinds of communication channels.</p>294<p>Transport objects are always instantiated by an295<a class="reference internal" href="asyncio-eventloop.html#asyncio-event-loop"><span class="std std-ref">asyncio event loop</span></a>.</p>296<p>asyncio implements transports for TCP, UDP, SSL, and subprocess pipes.297The methods available on a transport depend on the transport’s kind.</p>298<p>The transport classes are <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span class="std std-ref">not thread safe</span></a>.</p>299<section id="transports-hierarchy">300<h3>Transports Hierarchy<a class="headerlink" href="#transports-hierarchy" title="Link to this heading">¶</a></h3>301<dl class="py class">302<dt class="sig sig-object py" id="asyncio.BaseTransport">303<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">BaseTransport</span></span><a class="headerlink" href="#asyncio.BaseTransport" title="Link to this definition">¶</a></dt>304<dd><p>Base class for all transports. Contains methods that all305asyncio transports share.</p>306</dd></dl>307 308<dl class="py class">309<dt class="sig sig-object py" id="asyncio.WriteTransport">310<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">WriteTransport</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseTransport</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport" title="Link to this definition">¶</a></dt>311<dd><p>A base transport for write-only connections.</p>312<p>Instances of the <em>WriteTransport</em> class are returned from313the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.connect_write_pipe" title="asyncio.loop.connect_write_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.connect_write_pipe()</span></code></a> event loop method and314are also used by subprocess-related methods like315<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_exec()</span></code></a>.</p>316</dd></dl>317 318<dl class="py class">319<dt class="sig sig-object py" id="asyncio.ReadTransport">320<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">ReadTransport</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseTransport</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.ReadTransport" title="Link to this definition">¶</a></dt>321<dd><p>A base transport for read-only connections.</p>322<p>Instances of the <em>ReadTransport</em> class are returned from323the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.connect_read_pipe" title="asyncio.loop.connect_read_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.connect_read_pipe()</span></code></a> event loop method and324are also used by subprocess-related methods like325<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_exec()</span></code></a>.</p>326</dd></dl>327 328<dl class="py class">329<dt class="sig sig-object py" id="asyncio.Transport">330<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">Transport</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">WriteTransport</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ReadTransport</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Transport" title="Link to this definition">¶</a></dt>331<dd><p>Interface representing a bidirectional transport, such as a332TCP connection.</p>333<p>The user does not instantiate a transport directly; they call a334utility function, passing it a protocol factory and other335information necessary to create the transport and protocol.</p>336<p>Instances of the <em>Transport</em> class are returned from or used by337event loop methods like <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a>,338<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_unix_connection" title="asyncio.loop.create_unix_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_unix_connection()</span></code></a>,339<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a>, <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.sendfile" title="asyncio.loop.sendfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.sendfile()</span></code></a>, etc.</p>340</dd></dl>341 342<dl class="py class">343<dt class="sig sig-object py" id="asyncio.DatagramTransport">344<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">DatagramTransport</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseTransport</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramTransport" title="Link to this definition">¶</a></dt>345<dd><p>A transport for datagram (UDP) connections.</p>346<p>Instances of the <em>DatagramTransport</em> class are returned from347the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_datagram_endpoint" title="asyncio.loop.create_datagram_endpoint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_datagram_endpoint()</span></code></a> event loop method.</p>348</dd></dl>349 350<dl class="py class">351<dt class="sig sig-object py" id="asyncio.SubprocessTransport">352<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">SubprocessTransport</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseTransport</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport" title="Link to this definition">¶</a></dt>353<dd><p>An abstraction to represent a connection between a parent and its354child OS process.</p>355<p>Instances of the <em>SubprocessTransport</em> class are returned from356event loop methods <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_shell" title="asyncio.loop.subprocess_shell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_shell()</span></code></a> and357<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_exec()</span></code></a>.</p>358</dd></dl>359 360</section>361<section id="base-transport">362<h3>Base Transport<a class="headerlink" href="#base-transport" title="Link to this heading">¶</a></h3>363<dl class="py method">364<dt class="sig sig-object py" id="asyncio.BaseTransport.close">365<span class="sig-prename descclassname"><span class="pre">BaseTransport.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.close" title="Link to this definition">¶</a></dt>366<dd><p>Close the transport.</p>367<p>If the transport has a buffer for outgoing368data, buffered data will be flushed asynchronously. No more data369will be received. After all buffered data is flushed, the370protocol’s <a class="reference internal" href="#asyncio.BaseProtocol.connection_lost" title="asyncio.BaseProtocol.connection_lost"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.connection_lost()</span></code></a> method will be called with371<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> as its argument. The transport should not be372used once it is closed.</p>373</dd></dl>374 375<dl class="py method">376<dt class="sig sig-object py" id="asyncio.BaseTransport.is_closing">377<span class="sig-prename descclassname"><span class="pre">BaseTransport.</span></span><span class="sig-name descname"><span class="pre">is_closing</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.is_closing" title="Link to this definition">¶</a></dt>378<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the transport is closing or is closed.</p>379</dd></dl>380 381<dl class="py method">382<dt class="sig sig-object py" id="asyncio.BaseTransport.get_extra_info">383<span class="sig-prename descclassname"><span class="pre">BaseTransport.</span></span><span class="sig-name descname"><span class="pre">get_extra_info</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.get_extra_info" title="Link to this definition">¶</a></dt>384<dd><p>Return information about the transport or underlying resources385it uses.</p>386<p><em>name</em> is a string representing the piece of transport-specific387information to get.</p>388<p><em>default</em> is the value to return if the information is not389available, or if the transport does not support querying it390with the given third-party event loop implementation or on the391current platform.</p>392<p>For example, the following code attempts to get the underlying393socket object of the transport:</p>394<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sock</span> <span class="o">=</span> <span class="n">transport</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s1">'socket'</span><span class="p">)</span>395<span class="k">if</span> <span class="n">sock</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>396 <span class="nb">print</span><span class="p">(</span><span class="n">sock</span><span class="o">.</span><span class="n">getsockopt</span><span class="p">(</span><span class="o">...</span><span class="p">))</span>397</pre></div>398</div>399<p>Categories of information that can be queried on some transports:</p>400<ul class="simple">401<li><p>socket:</p>402<ul>403<li><p><code class="docutils literal notranslate"><span class="pre">'peername'</span></code>: the remote address to which the socket is404connected, result of <a class="reference internal" href="socket.html#socket.socket.getpeername" title="socket.socket.getpeername"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.getpeername()</span></code></a>405(<code class="docutils literal notranslate"><span class="pre">None</span></code> on error)</p></li>406<li><p><code class="docutils literal notranslate"><span class="pre">'socket'</span></code>: <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a> instance</p></li>407<li><p><code class="docutils literal notranslate"><span class="pre">'sockname'</span></code>: the socket’s own address,408result of <a class="reference internal" href="socket.html#socket.socket.getsockname" title="socket.socket.getsockname"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.getsockname()</span></code></a></p></li>409</ul>410</li>411<li><p>SSL socket:</p>412<ul>413<li><p><code class="docutils literal notranslate"><span class="pre">'compression'</span></code>: the compression algorithm being used as a414string, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the connection isn’t compressed; result415of <a class="reference internal" href="ssl.html#ssl.SSLSocket.compression" title="ssl.SSLSocket.compression"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLSocket.compression()</span></code></a></p></li>416<li><p><code class="docutils literal notranslate"><span class="pre">'cipher'</span></code>: a three-value tuple containing the name of the417cipher being used, the version of the SSL protocol that defines418its use, and the number of secret bits being used; result of419<a class="reference internal" href="ssl.html#ssl.SSLSocket.cipher" title="ssl.SSLSocket.cipher"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLSocket.cipher()</span></code></a></p></li>420<li><p><code class="docutils literal notranslate"><span class="pre">'peercert'</span></code>: peer certificate; result of421<a class="reference internal" href="ssl.html#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLSocket.getpeercert()</span></code></a></p></li>422<li><p><code class="docutils literal notranslate"><span class="pre">'sslcontext'</span></code>: <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> instance</p></li>423<li><p><code class="docutils literal notranslate"><span class="pre">'ssl_object'</span></code>: <a class="reference internal" href="ssl.html#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLObject</span></code></a> or424<a class="reference internal" href="ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLSocket</span></code></a> instance</p></li>425</ul>426</li>427<li><p>pipe:</p>428<ul>429<li><p><code class="docutils literal notranslate"><span class="pre">'pipe'</span></code>: pipe object</p></li>430</ul>431</li>432<li><p>subprocess:</p>433<ul>434<li><p><code class="docutils literal notranslate"><span class="pre">'subprocess'</span></code>: <a class="reference internal" href="subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a> instance</p></li>435</ul>436</li>437</ul>438</dd></dl>439 440<dl class="py method">441<dt class="sig sig-object py" id="asyncio.BaseTransport.set_protocol">442<span class="sig-prename descclassname"><span class="pre">BaseTransport.</span></span><span class="sig-name descname"><span class="pre">set_protocol</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.set_protocol" title="Link to this definition">¶</a></dt>443<dd><p>Set a new protocol.</p>444<p>Switching protocol should only be done when both445protocols are documented to support the switch.</p>446</dd></dl>447 448<dl class="py method">449<dt class="sig sig-object py" id="asyncio.BaseTransport.get_protocol">450<span class="sig-prename descclassname"><span class="pre">BaseTransport.</span></span><span class="sig-name descname"><span class="pre">get_protocol</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.get_protocol" title="Link to this definition">¶</a></dt>451<dd><p>Return the current protocol.</p>452</dd></dl>453 454</section>455<section id="read-only-transports">456<h3>Read-only Transports<a class="headerlink" href="#read-only-transports" title="Link to this heading">¶</a></h3>457<dl class="py method">458<dt class="sig sig-object py" id="asyncio.ReadTransport.is_reading">459<span class="sig-prename descclassname"><span class="pre">ReadTransport.</span></span><span class="sig-name descname"><span class="pre">is_reading</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.ReadTransport.is_reading" title="Link to this definition">¶</a></dt>460<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the transport is receiving new data.</p>461<div class="versionadded">462<p><span class="versionmodified added">Added in version 3.7.</span></p>463</div>464</dd></dl>465 466<dl class="py method">467<dt class="sig sig-object py" id="asyncio.ReadTransport.pause_reading">468<span class="sig-prename descclassname"><span class="pre">ReadTransport.</span></span><span class="sig-name descname"><span class="pre">pause_reading</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.ReadTransport.pause_reading" title="Link to this definition">¶</a></dt>469<dd><p>Pause the receiving end of the transport. No data will be passed to470the protocol’s <a class="reference internal" href="#asyncio.Protocol.data_received" title="asyncio.Protocol.data_received"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.data_received()</span></code></a>471method until <a class="reference internal" href="#asyncio.ReadTransport.resume_reading" title="asyncio.ReadTransport.resume_reading"><code class="xref py py-meth docutils literal notranslate"><span class="pre">resume_reading()</span></code></a> is called.</p>472<div class="versionchanged">473<p><span class="versionmodified changed">Changed in version 3.7: </span>The method is idempotent, i.e. it can be called when the474transport is already paused or closed.</p>475</div>476</dd></dl>477 478<dl class="py method">479<dt class="sig sig-object py" id="asyncio.ReadTransport.resume_reading">480<span class="sig-prename descclassname"><span class="pre">ReadTransport.</span></span><span class="sig-name descname"><span class="pre">resume_reading</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.ReadTransport.resume_reading" title="Link to this definition">¶</a></dt>481<dd><p>Resume the receiving end. The protocol’s482<a class="reference internal" href="#asyncio.Protocol.data_received" title="asyncio.Protocol.data_received"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.data_received()</span></code></a> method483will be called once again if some data is available for reading.</p>484<div class="versionchanged">485<p><span class="versionmodified changed">Changed in version 3.7: </span>The method is idempotent, i.e. it can be called when the486transport is already reading.</p>487</div>488</dd></dl>489 490</section>491<section id="write-only-transports">492<h3>Write-only Transports<a class="headerlink" href="#write-only-transports" title="Link to this heading">¶</a></h3>493<dl class="py method">494<dt class="sig sig-object py" id="asyncio.WriteTransport.abort">495<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">abort</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.abort" title="Link to this definition">¶</a></dt>496<dd><p>Close the transport immediately, without waiting for pending operations497to complete. Buffered data will be lost. No more data will be received.498The protocol’s <a class="reference internal" href="#asyncio.BaseProtocol.connection_lost" title="asyncio.BaseProtocol.connection_lost"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.connection_lost()</span></code></a> method will eventually be499called with <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> as its argument.</p>500</dd></dl>501 502<dl class="py method">503<dt class="sig sig-object py" id="asyncio.WriteTransport.can_write_eof">504<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">can_write_eof</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.can_write_eof" title="Link to this definition">¶</a></dt>505<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if the transport supports506<a class="reference internal" href="#asyncio.WriteTransport.write_eof" title="asyncio.WriteTransport.write_eof"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write_eof()</span></code></a>, <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> if not.</p>507</dd></dl>508 509<dl class="py method">510<dt class="sig sig-object py" id="asyncio.WriteTransport.get_write_buffer_size">511<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">get_write_buffer_size</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.get_write_buffer_size" title="Link to this definition">¶</a></dt>512<dd><p>Return the current size of the output buffer used by the transport.</p>513</dd></dl>514 515<dl class="py method">516<dt class="sig sig-object py" id="asyncio.WriteTransport.get_write_buffer_limits">517<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">get_write_buffer_limits</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.get_write_buffer_limits" title="Link to this definition">¶</a></dt>518<dd><p>Get the <em>high</em> and <em>low</em> watermarks for write flow control. Return a519tuple <code class="docutils literal notranslate"><span class="pre">(low,</span> <span class="pre">high)</span></code> where <em>low</em> and <em>high</em> are positive number of520bytes.</p>521<p>Use <a class="reference internal" href="#asyncio.WriteTransport.set_write_buffer_limits" title="asyncio.WriteTransport.set_write_buffer_limits"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_write_buffer_limits()</span></code></a> to set the limits.</p>522<div class="versionadded">523<p><span class="versionmodified added">Added in version 3.4.2.</span></p>524</div>525</dd></dl>526 527<dl class="py method">528<dt class="sig sig-object py" id="asyncio.WriteTransport.set_write_buffer_limits">529<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">set_write_buffer_limits</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">high</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">low</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.set_write_buffer_limits" title="Link to this definition">¶</a></dt>530<dd><p>Set the <em>high</em> and <em>low</em> watermarks for write flow control.</p>531<p>These two values (measured in number of532bytes) control when the protocol’s533<a class="reference internal" href="#asyncio.BaseProtocol.pause_writing" title="asyncio.BaseProtocol.pause_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.pause_writing()</span></code></a>534and <a class="reference internal" href="#asyncio.BaseProtocol.resume_writing" title="asyncio.BaseProtocol.resume_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.resume_writing()</span></code></a>535methods are called. If specified, the low watermark must be less536than or equal to the high watermark. Neither <em>high</em> nor <em>low</em>537can be negative.</p>538<p><a class="reference internal" href="#asyncio.BaseProtocol.pause_writing" title="asyncio.BaseProtocol.pause_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pause_writing()</span></code></a> is called when the buffer size539becomes greater than or equal to the <em>high</em> value. If writing has540been paused, <a class="reference internal" href="#asyncio.BaseProtocol.resume_writing" title="asyncio.BaseProtocol.resume_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">resume_writing()</span></code></a> is called when541the buffer size becomes less than or equal to the <em>low</em> value.</p>542<p>The defaults are implementation-specific. If only the543high watermark is given, the low watermark defaults to an544implementation-specific value less than or equal to the545high watermark. Setting <em>high</em> to zero forces <em>low</em> to zero as546well, and causes <a class="reference internal" href="#asyncio.BaseProtocol.pause_writing" title="asyncio.BaseProtocol.pause_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pause_writing()</span></code></a> to be called547whenever the buffer becomes non-empty. Setting <em>low</em> to zero causes548<a class="reference internal" href="#asyncio.BaseProtocol.resume_writing" title="asyncio.BaseProtocol.resume_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">resume_writing()</span></code></a> to be called only once the549buffer is empty. Use of zero for either limit is generally550sub-optimal as it reduces opportunities for doing I/O and551computation concurrently.</p>552<p>Use <a class="reference internal" href="#asyncio.WriteTransport.get_write_buffer_limits" title="asyncio.WriteTransport.get_write_buffer_limits"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_write_buffer_limits()</span></code></a>553to get the limits.</p>554</dd></dl>555 556<dl class="py method">557<dt class="sig sig-object py" id="asyncio.WriteTransport.write">558<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.write" title="Link to this definition">¶</a></dt>559<dd><p>Write some <em>data</em> bytes to the transport.</p>560<p>This method does not block; it buffers the data and arranges for it561to be sent out asynchronously.</p>562</dd></dl>563 564<dl class="py method">565<dt class="sig sig-object py" id="asyncio.WriteTransport.writelines">566<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">writelines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">list_of_data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.writelines" title="Link to this definition">¶</a></dt>567<dd><p>Write a list (or any iterable) of data bytes to the transport.568This is functionally equivalent to calling <a class="reference internal" href="#asyncio.WriteTransport.write" title="asyncio.WriteTransport.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> on each569element yielded by the iterable, but may be implemented more570efficiently.</p>571</dd></dl>572 573<dl class="py method">574<dt class="sig sig-object py" id="asyncio.WriteTransport.write_eof">575<span class="sig-prename descclassname"><span class="pre">WriteTransport.</span></span><span class="sig-name descname"><span class="pre">write_eof</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.write_eof" title="Link to this definition">¶</a></dt>576<dd><p>Close the write end of the transport after flushing all buffered data.577Data may still be received.</p>578<p>This method can raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> if the transport579(e.g. SSL) doesn’t support half-closed connections.</p>580</dd></dl>581 582</section>583<section id="datagram-transports">584<h3>Datagram Transports<a class="headerlink" href="#datagram-transports" title="Link to this heading">¶</a></h3>585<dl class="py method">586<dt class="sig sig-object py" id="asyncio.DatagramTransport.sendto">587<span class="sig-prename descclassname"><span class="pre">DatagramTransport.</span></span><span class="sig-name descname"><span class="pre">sendto</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">addr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramTransport.sendto" title="Link to this definition">¶</a></dt>588<dd><p>Send the <em>data</em> bytes to the remote peer given by <em>addr</em> (a589transport-dependent target address). If <em>addr</em> is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>,590the data is sent to the target address given on transport591creation.</p>592<p>This method does not block; it buffers the data and arranges593for it to be sent out asynchronously.</p>594<div class="versionchanged">595<p><span class="versionmodified changed">Changed in version 3.13: </span>This method can be called with an empty bytes object to send a596zero-length datagram. The buffer size calculation used for flow597control is also updated to account for the datagram header.</p>598</div>599</dd></dl>600 601<dl class="py method">602<dt class="sig sig-object py" id="asyncio.DatagramTransport.abort">603<span class="sig-prename descclassname"><span class="pre">DatagramTransport.</span></span><span class="sig-name descname"><span class="pre">abort</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramTransport.abort" title="Link to this definition">¶</a></dt>604<dd><p>Close the transport immediately, without waiting for pending605operations to complete. Buffered data will be lost.606No more data will be received. The protocol’s607<a class="reference internal" href="#asyncio.BaseProtocol.connection_lost" title="asyncio.BaseProtocol.connection_lost"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.connection_lost()</span></code></a>608method will eventually be called with <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> as its argument.</p>609</dd></dl>610 611</section>612<section id="subprocess-transports">613<span id="asyncio-subprocess-transports"></span><h3>Subprocess Transports<a class="headerlink" href="#subprocess-transports" title="Link to this heading">¶</a></h3>614<dl class="py method">615<dt class="sig sig-object py" id="asyncio.SubprocessTransport.get_pid">616<span class="sig-prename descclassname"><span class="pre">SubprocessTransport.</span></span><span class="sig-name descname"><span class="pre">get_pid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport.get_pid" title="Link to this definition">¶</a></dt>617<dd><p>Return the subprocess process id as an integer.</p>618</dd></dl>619 620<dl class="py method">621<dt class="sig sig-object py" id="asyncio.SubprocessTransport.get_pipe_transport">622<span class="sig-prename descclassname"><span class="pre">SubprocessTransport.</span></span><span class="sig-name descname"><span class="pre">get_pipe_transport</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport.get_pipe_transport" title="Link to this definition">¶</a></dt>623<dd><p>Return the transport for the communication pipe corresponding to the624integer file descriptor <em>fd</em>:</p>625<ul class="simple">626<li><p><code class="docutils literal notranslate"><span class="pre">0</span></code>: writable streaming transport of the standard input (<em>stdin</em>),627or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if the subprocess was not created with <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code></p></li>628<li><p><code class="docutils literal notranslate"><span class="pre">1</span></code>: readable streaming transport of the standard output (<em>stdout</em>),629or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if the subprocess was not created with <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code></p></li>630<li><p><code class="docutils literal notranslate"><span class="pre">2</span></code>: readable streaming transport of the standard error (<em>stderr</em>),631or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if the subprocess was not created with <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code></p></li>632<li><p>other <em>fd</em>: <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a></p></li>633</ul>634</dd></dl>635 636<dl class="py method">637<dt class="sig sig-object py" id="asyncio.SubprocessTransport.get_returncode">638<span class="sig-prename descclassname"><span class="pre">SubprocessTransport.</span></span><span class="sig-name descname"><span class="pre">get_returncode</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport.get_returncode" title="Link to this definition">¶</a></dt>639<dd><p>Return the subprocess return code as an integer or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>640if it hasn’t returned, which is similar to the641<a class="reference internal" href="subprocess.html#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">subprocess.Popen.returncode</span></code></a> attribute.</p>642</dd></dl>643 644<dl class="py method">645<dt class="sig sig-object py" id="asyncio.SubprocessTransport.kill">646<span class="sig-prename descclassname"><span class="pre">SubprocessTransport.</span></span><span class="sig-name descname"><span class="pre">kill</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport.kill" title="Link to this definition">¶</a></dt>647<dd><p>Kill the subprocess.</p>648<p>On POSIX systems, the function sends SIGKILL to the subprocess.649On Windows, this method is an alias for <a class="reference internal" href="#asyncio.SubprocessTransport.terminate" title="asyncio.SubprocessTransport.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a>.</p>650<p>See also <a class="reference internal" href="subprocess.html#subprocess.Popen.kill" title="subprocess.Popen.kill"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subprocess.Popen.kill()</span></code></a>.</p>651</dd></dl>652 653<dl class="py method">654<dt class="sig sig-object py" id="asyncio.SubprocessTransport.send_signal">655<span class="sig-prename descclassname"><span class="pre">SubprocessTransport.</span></span><span class="sig-name descname"><span class="pre">send_signal</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">signal</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport.send_signal" title="Link to this definition">¶</a></dt>656<dd><p>Send the <em>signal</em> number to the subprocess, as in657<a class="reference internal" href="subprocess.html#subprocess.Popen.send_signal" title="subprocess.Popen.send_signal"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subprocess.Popen.send_signal()</span></code></a>.</p>658</dd></dl>659 660<dl class="py method">661<dt class="sig sig-object py" id="asyncio.SubprocessTransport.terminate">662<span class="sig-prename descclassname"><span class="pre">SubprocessTransport.</span></span><span class="sig-name descname"><span class="pre">terminate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport.terminate" title="Link to this definition">¶</a></dt>663<dd><p>Stop the subprocess.</p>664<p>On POSIX systems, this method sends <a class="reference internal" href="signal.html#signal.SIGTERM" title="signal.SIGTERM"><code class="xref py py-const docutils literal notranslate"><span class="pre">SIGTERM</span></code></a> to the subprocess.665On Windows, the Windows API function <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> is called to666stop the subprocess.</p>667<p>See also <a class="reference internal" href="subprocess.html#subprocess.Popen.terminate" title="subprocess.Popen.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subprocess.Popen.terminate()</span></code></a>.</p>668</dd></dl>669 670<dl class="py method">671<dt class="sig sig-object py" id="asyncio.SubprocessTransport.close">672<span class="sig-prename descclassname"><span class="pre">SubprocessTransport.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessTransport.close" title="Link to this definition">¶</a></dt>673<dd><p>Kill the subprocess by calling the <a class="reference internal" href="#asyncio.SubprocessTransport.kill" title="asyncio.SubprocessTransport.kill"><code class="xref py py-meth docutils literal notranslate"><span class="pre">kill()</span></code></a> method.</p>674<p>If the subprocess hasn’t returned yet, and close transports of675<em>stdin</em>, <em>stdout</em>, and <em>stderr</em> pipes.</p>676</dd></dl>677 678</section>679</section>680<section id="protocols">681<span id="asyncio-protocol"></span><h2>Protocols<a class="headerlink" href="#protocols" title="Link to this heading">¶</a></h2>682<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/asyncio/protocols.py">Lib/asyncio/protocols.py</a></p>683<hr class="docutils" />684<p>asyncio provides a set of abstract base classes that should be used685to implement network protocols. Those classes are meant to be used686together with <a class="reference internal" href="#asyncio-transport"><span class="std std-ref">transports</span></a>.</p>687<p>Subclasses of abstract base protocol classes may implement some or688all methods. All these methods are callbacks: they are called by689transports on certain events, for example when some data is received.690A base protocol method should be called by the corresponding transport.</p>691<section id="base-protocols">692<h3>Base Protocols<a class="headerlink" href="#base-protocols" title="Link to this heading">¶</a></h3>693<dl class="py class">694<dt class="sig sig-object py" id="asyncio.BaseProtocol">695<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">BaseProtocol</span></span><a class="headerlink" href="#asyncio.BaseProtocol" title="Link to this definition">¶</a></dt>696<dd><p>Base protocol with methods that all protocols share.</p>697</dd></dl>698 699<dl class="py class">700<dt class="sig sig-object py" id="asyncio.Protocol">701<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">Protocol</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseProtocol</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Protocol" title="Link to this definition">¶</a></dt>702<dd><p>The base class for implementing streaming protocols703(TCP, Unix sockets, etc).</p>704</dd></dl>705 706<dl class="py class">707<dt class="sig sig-object py" id="asyncio.BufferedProtocol">708<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">BufferedProtocol</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseProtocol</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BufferedProtocol" title="Link to this definition">¶</a></dt>709<dd><p>A base class for implementing streaming protocols with manual710control of the receive buffer.</p>711</dd></dl>712 713<dl class="py class">714<dt class="sig sig-object py" id="asyncio.DatagramProtocol">715<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">DatagramProtocol</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseProtocol</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramProtocol" title="Link to this definition">¶</a></dt>716<dd><p>The base class for implementing datagram (UDP) protocols.</p>717</dd></dl>718 719<dl class="py class">720<dt class="sig sig-object py" id="asyncio.SubprocessProtocol">721<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">SubprocessProtocol</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseProtocol</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessProtocol" title="Link to this definition">¶</a></dt>722<dd><p>The base class for implementing protocols communicating with child723processes (unidirectional pipes).</p>724</dd></dl>725 726</section>727<section id="base-protocol">728<h3>Base Protocol<a class="headerlink" href="#base-protocol" title="Link to this heading">¶</a></h3>729<p>All asyncio protocols can implement Base Protocol callbacks.</p>730<p class="rubric">Connection Callbacks</p>731<p>Connection callbacks are called on all protocols, exactly once per732a successful connection. All other protocol callbacks can only be733called between those two methods.</p>734<dl class="py method">735<dt class="sig sig-object py" id="asyncio.BaseProtocol.connection_made">736<span class="sig-prename descclassname"><span class="pre">BaseProtocol.</span></span><span class="sig-name descname"><span class="pre">connection_made</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">transport</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.connection_made" title="Link to this definition">¶</a></dt>737<dd><p>Called when a connection is made.</p>738<p>The <em>transport</em> argument is the transport representing the739connection. The protocol is responsible for storing the reference740to its transport.</p>741</dd></dl>742 743<dl class="py method">744<dt class="sig sig-object py" id="asyncio.BaseProtocol.connection_lost">745<span class="sig-prename descclassname"><span class="pre">BaseProtocol.</span></span><span class="sig-name descname"><span class="pre">connection_lost</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.connection_lost" title="Link to this definition">¶</a></dt>746<dd><p>Called when the connection is lost or closed.</p>747<p>The argument is either an exception object or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.748The latter means a regular EOF is received, or the connection was749aborted or closed by this side of the connection.</p>750</dd></dl>751 752<p class="rubric">Flow Control Callbacks</p>753<p>Flow control callbacks can be called by transports to pause or754resume writing performed by the protocol.</p>755<p>See the documentation of the <a class="reference internal" href="#asyncio.WriteTransport.set_write_buffer_limits" title="asyncio.WriteTransport.set_write_buffer_limits"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_write_buffer_limits()</span></code></a>756method for more details.</p>757<dl class="py method">758<dt class="sig sig-object py" id="asyncio.BaseProtocol.pause_writing">759<span class="sig-prename descclassname"><span class="pre">BaseProtocol.</span></span><span class="sig-name descname"><span class="pre">pause_writing</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.pause_writing" title="Link to this definition">¶</a></dt>760<dd><p>Called when the transport’s buffer goes over the high watermark.</p>761</dd></dl>762 763<dl class="py method">764<dt class="sig sig-object py" id="asyncio.BaseProtocol.resume_writing">765<span class="sig-prename descclassname"><span class="pre">BaseProtocol.</span></span><span class="sig-name descname"><span class="pre">resume_writing</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.resume_writing" title="Link to this definition">¶</a></dt>766<dd><p>Called when the transport’s buffer drains below the low watermark.</p>767</dd></dl>768 769<p>If the buffer size equals the high watermark,770<a class="reference internal" href="#asyncio.BaseProtocol.pause_writing" title="asyncio.BaseProtocol.pause_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pause_writing()</span></code></a> is not called: the buffer size must771go strictly over.</p>772<p>Conversely, <a class="reference internal" href="#asyncio.BaseProtocol.resume_writing" title="asyncio.BaseProtocol.resume_writing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">resume_writing()</span></code></a> is called when the773buffer size is equal or lower than the low watermark. These end774conditions are important to ensure that things go as expected when775either mark is zero.</p>776</section>777<section id="streaming-protocols">778<h3>Streaming Protocols<a class="headerlink" href="#streaming-protocols" title="Link to this heading">¶</a></h3>779<p>Event methods, such as <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a>,780<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_unix_server" title="asyncio.loop.create_unix_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_unix_server()</span></code></a>, <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a>,781<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_unix_connection" title="asyncio.loop.create_unix_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_unix_connection()</span></code></a>, <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.connect_accepted_socket" title="asyncio.loop.connect_accepted_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.connect_accepted_socket()</span></code></a>,782<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.connect_read_pipe" title="asyncio.loop.connect_read_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.connect_read_pipe()</span></code></a>, and <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.connect_write_pipe" title="asyncio.loop.connect_write_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.connect_write_pipe()</span></code></a>783accept factories that return streaming protocols.</p>784<dl class="py method">785<dt class="sig sig-object py" id="asyncio.Protocol.data_received">786<span class="sig-prename descclassname"><span class="pre">Protocol.</span></span><span class="sig-name descname"><span class="pre">data_received</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Protocol.data_received" title="Link to this definition">¶</a></dt>787<dd><p>Called when some data is received. <em>data</em> is a non-empty bytes788object containing the incoming data.</p>789<p>Whether the data is buffered, chunked or reassembled depends on790the transport. In general, you shouldn’t rely on specific semantics791and instead make your parsing generic and flexible. However,792data is always received in the correct order.</p>793<p>The method can be called an arbitrary number of times while794a connection is open.</p>795<p>However, <a class="reference internal" href="#asyncio.Protocol.eof_received" title="asyncio.Protocol.eof_received"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.eof_received()</span></code></a>796is called at most once. Once <code class="docutils literal notranslate"><span class="pre">eof_received()</span></code> is called,797<code class="docutils literal notranslate"><span class="pre">data_received()</span></code> is not called anymore.</p>798</dd></dl>799 800<dl class="py method">801<dt class="sig sig-object py" id="asyncio.Protocol.eof_received">802<span class="sig-prename descclassname"><span class="pre">Protocol.</span></span><span class="sig-name descname"><span class="pre">eof_received</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Protocol.eof_received" title="Link to this definition">¶</a></dt>803<dd><p>Called when the other end signals it won’t send any more data804(for example by calling <a class="reference internal" href="#asyncio.WriteTransport.write_eof" title="asyncio.WriteTransport.write_eof"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transport.write_eof()</span></code></a>, if the other end also uses805asyncio).</p>806<p>This method may return a false value (including <code class="docutils literal notranslate"><span class="pre">None</span></code>), in which case807the transport will close itself. Conversely, if this method returns a808true value, the protocol used determines whether to close the transport.809Since the default implementation returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, it implicitly closes the810connection.</p>811<p>Some transports, including SSL, don’t support half-closed connections,812in which case returning true from this method will result in the connection813being closed.</p>814</dd></dl>815 816<p>State machine:</p>817<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>start -> connection_made818 [-> data_received]*819 [-> eof_received]?820-> connection_lost -> end821</pre></div>822</div>823</section>824<section id="buffered-streaming-protocols">825<h3>Buffered Streaming Protocols<a class="headerlink" href="#buffered-streaming-protocols" title="Link to this heading">¶</a></h3>826<div class="versionadded">827<p><span class="versionmodified added">Added in version 3.7.</span></p>828</div>829<p>Buffered Protocols can be used with any event loop method830that supports <a class="reference internal" href="#streaming-protocols">Streaming Protocols</a>.</p>831<p><code class="docutils literal notranslate"><span class="pre">BufferedProtocol</span></code> implementations allow explicit manual allocation832and control of the receive buffer. Event loops can then use the buffer833provided by the protocol to avoid unnecessary data copies. This834can result in noticeable performance improvement for protocols that835receive big amounts of data. Sophisticated protocol implementations836can significantly reduce the number of buffer allocations.</p>837<p>The following callbacks are called on <a class="reference internal" href="#asyncio.BufferedProtocol" title="asyncio.BufferedProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedProtocol</span></code></a>838instances:</p>839<dl class="py method">840<dt class="sig sig-object py" id="asyncio.BufferedProtocol.get_buffer">841<span class="sig-prename descclassname"><span class="pre">BufferedProtocol.</span></span><span class="sig-name descname"><span class="pre">get_buffer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sizehint</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BufferedProtocol.get_buffer" title="Link to this definition">¶</a></dt>842<dd><p>Called to allocate a new receive buffer.</p>843<p><em>sizehint</em> is the recommended minimum size for the returned844buffer. It is acceptable to return smaller or larger buffers845than what <em>sizehint</em> suggests. When set to -1, the buffer size846can be arbitrary. It is an error to return a buffer with a zero size.</p>847<p><code class="docutils literal notranslate"><span class="pre">get_buffer()</span></code> must return an object implementing the848<a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer protocol</span></a>.</p>849</dd></dl>850 851<dl class="py method">852<dt class="sig sig-object py" id="asyncio.BufferedProtocol.buffer_updated">853<span class="sig-prename descclassname"><span class="pre">BufferedProtocol.</span></span><span class="sig-name descname"><span class="pre">buffer_updated</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">nbytes</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BufferedProtocol.buffer_updated" title="Link to this definition">¶</a></dt>854<dd><p>Called when the buffer was updated with the received data.</p>855<p><em>nbytes</em> is the total number of bytes that were written to the buffer.</p>856</dd></dl>857 858<dl class="py method">859<dt class="sig sig-object py" id="asyncio.BufferedProtocol.eof_received">860<span class="sig-prename descclassname"><span class="pre">BufferedProtocol.</span></span><span class="sig-name descname"><span class="pre">eof_received</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BufferedProtocol.eof_received" title="Link to this definition">¶</a></dt>861<dd><p>See the documentation of the <a class="reference internal" href="#asyncio.Protocol.eof_received" title="asyncio.Protocol.eof_received"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.eof_received()</span></code></a> method.</p>862</dd></dl>863 864<p><a class="reference internal" href="#asyncio.BufferedProtocol.get_buffer" title="asyncio.BufferedProtocol.get_buffer"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_buffer()</span></code></a> can be called an arbitrary number865of times during a connection. However, <a class="reference internal" href="#asyncio.Protocol.eof_received" title="asyncio.Protocol.eof_received"><code class="xref py py-meth docutils literal notranslate"><span class="pre">protocol.eof_received()</span></code></a> is called at most once866and, if called, <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_buffer()</span></code> and867<a class="reference internal" href="#asyncio.BufferedProtocol.buffer_updated" title="asyncio.BufferedProtocol.buffer_updated"><code class="xref py py-meth docutils literal notranslate"><span class="pre">buffer_updated()</span></code></a> won’t be called after it.</p>868<p>State machine:</p>869<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>start -> connection_made870 [-> get_buffer871 [-> buffer_updated]?872 ]*873 [-> eof_received]?874-> connection_lost -> end875</pre></div>876</div>877</section>878<section id="datagram-protocols">879<h3>Datagram Protocols<a class="headerlink" href="#datagram-protocols" title="Link to this heading">¶</a></h3>880<p>Datagram Protocol instances should be constructed by protocol881factories passed to the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_datagram_endpoint" title="asyncio.loop.create_datagram_endpoint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_datagram_endpoint()</span></code></a> method.</p>882<dl class="py method">883<dt class="sig sig-object py" id="asyncio.DatagramProtocol.datagram_received">884<span class="sig-prename descclassname"><span class="pre">DatagramProtocol.</span></span><span class="sig-name descname"><span class="pre">datagram_received</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">addr</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramProtocol.datagram_received" title="Link to this definition">¶</a></dt>885<dd><p>Called when a datagram is received. <em>data</em> is a bytes object containing886the incoming data. <em>addr</em> is the address of the peer sending the data;887the exact format depends on the transport.</p>888</dd></dl>889 890<dl class="py method">891<dt class="sig sig-object py" id="asyncio.DatagramProtocol.error_received">892<span class="sig-prename descclassname"><span class="pre">DatagramProtocol.</span></span><span class="sig-name descname"><span class="pre">error_received</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramProtocol.error_received" title="Link to this definition">¶</a></dt>893<dd><p>Called when a previous send or receive operation raises an894<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSError</span></code></a>. <em>exc</em> is the <code class="xref py py-class docutils literal notranslate"><span class="pre">OSError</span></code> instance.</p>895<p>This method is called in rare conditions, when the transport (e.g. UDP)896detects that a datagram could not be delivered to its recipient.897In many conditions though, undeliverable datagrams will be silently898dropped.</p>899</dd></dl>900 901<div class="admonition note">902<p class="admonition-title">Note</p>903<p>On BSD systems (macOS, FreeBSD, etc.) flow control is not supported904for datagram protocols, because there is no reliable way to detect send905failures caused by writing too many packets.</p>906<p>The socket always appears ‘ready’ and excess packets are dropped. An907<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSError</span></code></a> with <code class="docutils literal notranslate"><span class="pre">errno</span></code> set to <a class="reference internal" href="errno.html#errno.ENOBUFS" title="errno.ENOBUFS"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.ENOBUFS</span></code></a> may908or may not be raised; if it is raised, it will be reported to909<a class="reference internal" href="#asyncio.DatagramProtocol.error_received" title="asyncio.DatagramProtocol.error_received"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DatagramProtocol.error_received()</span></code></a> but otherwise ignored.</p>910</div>911</section>912<section id="subprocess-protocols">913<span id="asyncio-subprocess-protocols"></span><h3>Subprocess Protocols<a class="headerlink" href="#subprocess-protocols" title="Link to this heading">¶</a></h3>914<p>Subprocess Protocol instances should be constructed by protocol915factories passed to the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_exec()</span></code></a> and916<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_shell" title="asyncio.loop.subprocess_shell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_shell()</span></code></a> methods.</p>917<dl class="py method">918<dt class="sig sig-object py" id="asyncio.SubprocessProtocol.pipe_data_received">919<span class="sig-prename descclassname"><span class="pre">SubprocessProtocol.</span></span><span class="sig-name descname"><span class="pre">pipe_data_received</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessProtocol.pipe_data_received" title="Link to this definition">¶</a></dt>920<dd><p>Called when the child process writes data into its stdout or stderr921pipe.</p>922<p><em>fd</em> is the integer file descriptor of the pipe.</p>923<p><em>data</em> is a non-empty bytes object containing the received data.</p>924</dd></dl>925 926<dl class="py method">927<dt class="sig sig-object py" id="asyncio.SubprocessProtocol.pipe_connection_lost">928<span class="sig-prename descclassname"><span class="pre">SubprocessProtocol.</span></span><span class="sig-name descname"><span class="pre">pipe_connection_lost</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessProtocol.pipe_connection_lost" title="Link to this definition">¶</a></dt>929<dd><p>Called when one of the pipes communicating with the child process930is closed.</p>931<p><em>fd</em> is the integer file descriptor that was closed.</p>932</dd></dl>933 934<dl class="py method">935<dt class="sig sig-object py" id="asyncio.SubprocessProtocol.process_exited">936<span class="sig-prename descclassname"><span class="pre">SubprocessProtocol.</span></span><span class="sig-name descname"><span class="pre">process_exited</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessProtocol.process_exited" title="Link to this definition">¶</a></dt>937<dd><p>Called when the child process has exited.</p>938<p>It can be called before <a class="reference internal" href="#asyncio.SubprocessProtocol.pipe_data_received" title="asyncio.SubprocessProtocol.pipe_data_received"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pipe_data_received()</span></code></a> and939<a class="reference internal" href="#asyncio.SubprocessProtocol.pipe_connection_lost" title="asyncio.SubprocessProtocol.pipe_connection_lost"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pipe_connection_lost()</span></code></a> methods.</p>940</dd></dl>941 942</section>943</section>944<section id="examples">945<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>946<section id="tcp-echo-server">947<span id="asyncio-example-tcp-echo-server-protocol"></span><h3>TCP Echo Server<a class="headerlink" href="#tcp-echo-server" title="Link to this heading">¶</a></h3>948<p>Create a TCP echo server using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a> method, send back949received data, and close the connection:</p>950<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>951 952 953<span class="k">class</span><span class="w"> </span><span class="nc">EchoServerProtocol</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">Protocol</span><span class="p">):</span>954 <span class="k">def</span><span class="w"> </span><span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transport</span><span class="p">):</span>955 <span class="n">peername</span> <span class="o">=</span> <span class="n">transport</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s1">'peername'</span><span class="p">)</span>956 <span class="nb">print</span><span class="p">(</span><span class="s1">'Connection from </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">peername</span><span class="p">))</span>957 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>958 959 <span class="k">def</span><span class="w"> </span><span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>960 <span class="n">message</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span>961 <span class="nb">print</span><span class="p">(</span><span class="s1">'Data received: </span><span class="si">{!r}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>962 963 <span class="nb">print</span><span class="p">(</span><span class="s1">'Send: </span><span class="si">{!r}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>964 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>965 966 <span class="nb">print</span><span class="p">(</span><span class="s1">'Close the client socket'</span><span class="p">)</span>967 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>968 969 970<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>971 <span class="c1"># Get a reference to the event loop as we plan to use</span>972 <span class="c1"># low-level APIs.</span>973 <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>974 975 <span class="n">server</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span>976 <span class="n">EchoServerProtocol</span><span class="p">,</span>977 <span class="s1">'127.0.0.1'</span><span class="p">,</span> <span class="mi">8888</span><span class="p">)</span>978 979 <span class="k">async</span> <span class="k">with</span> <span class="n">server</span><span class="p">:</span>980 <span class="k">await</span> <span class="n">server</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>981 982 983<span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>984</pre></div>985</div>986<div class="admonition seealso">987<p class="admonition-title">See also</p>988<p>The <a class="reference internal" href="asyncio-stream.html#asyncio-tcp-echo-server-streams"><span class="std std-ref">TCP echo server using streams</span></a>989example uses the high-level <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.start_server()</span></code></a> function.</p>990</div>991</section>992<section id="tcp-echo-client">993<span id="asyncio-example-tcp-echo-client-protocol"></span><h3>TCP Echo Client<a class="headerlink" href="#tcp-echo-client" title="Link to this heading">¶</a></h3>994<p>A TCP echo client using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a> method, sends995data, and waits until the connection is closed:</p>996<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>997 998 999<span class="k">class</span><span class="w"> </span><span class="nc">EchoClientProtocol</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">Protocol</span><span class="p">):</span>1000 <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">on_con_lost</span><span class="p">):</span>1001 <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>1002 <span class="bp">self</span><span class="o">.</span><span class="n">on_con_lost</span> <span class="o">=</span> <span class="n">on_con_lost</span>1003 1004 <span class="k">def</span><span class="w"> </span><span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transport</span><span class="p">):</span>1005 <span class="n">transport</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>1006 <span class="nb">print</span><span class="p">(</span><span class="s1">'Data sent: </span><span class="si">{!r}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">))</span>1007 1008 <span class="k">def</span><span class="w"> </span><span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>1009 <span class="nb">print</span><span class="p">(</span><span class="s1">'Data received: </span><span class="si">{!r}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">()))</span>1010 1011 <span class="k">def</span><span class="w"> </span><span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>1012 <span class="nb">print</span><span class="p">(</span><span class="s1">'The server closed the connection'</span><span class="p">)</span>1013 <span class="bp">self</span><span class="o">.</span><span class="n">on_con_lost</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>1014 1015 1016<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>1017 <span class="c1"># Get a reference to the event loop as we plan to use</span>1018 <span class="c1"># low-level APIs.</span>1019 <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>1020 1021 <span class="n">on_con_lost</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_future</span><span class="p">()</span>1022 <span class="n">message</span> <span class="o">=</span> <span class="s1">'Hello World!'</span>1023 1024 <span class="n">transport</span><span class="p">,</span> <span class="n">protocol</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span>1025 <span class="k">lambda</span><span class="p">:</span> <span class="n">EchoClientProtocol</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">on_con_lost</span><span class="p">),</span>1026 <span class="s1">'127.0.0.1'</span><span class="p">,</span> <span class="mi">8888</span><span class="p">)</span>1027 1028 <span class="c1"># Wait until the protocol signals that the connection</span>1029 <span class="c1"># is lost and close the transport.</span>1030 <span class="k">try</span><span class="p">:</span>1031 <span class="k">await</span> <span class="n">on_con_lost</span>1032 <span class="k">finally</span><span class="p">:</span>1033 <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>1034 1035 1036<span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>1037</pre></div>1038</div>1039<div class="admonition seealso">1040<p class="admonition-title">See also</p>1041<p>The <a class="reference internal" href="asyncio-stream.html#asyncio-tcp-echo-client-streams"><span class="std std-ref">TCP echo client using streams</span></a>1042example uses the high-level <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.open_connection()</span></code></a> function.</p>1043</div>1044</section>1045<section id="udp-echo-server">1046<span id="asyncio-udp-echo-server-protocol"></span><h3>UDP Echo Server<a class="headerlink" href="#udp-echo-server" title="Link to this heading">¶</a></h3>1047<p>A UDP echo server, using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_datagram_endpoint" title="asyncio.loop.create_datagram_endpoint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_datagram_endpoint()</span></code></a>1048method, sends back received data:</p>1049<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>1050 1051 1052<span class="k">class</span><span class="w"> </span><span class="nc">EchoServerProtocol</span><span class="p">:</span>1053 <span class="k">def</span><span class="w"> </span><span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transport</span><span class="p">):</span>1054 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>1055 1056 <span class="k">def</span><span class="w"> </span><span class="nf">datagram_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">addr</span><span class="p">):</span>1057 <span class="n">message</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span>1058 <span class="nb">print</span><span class="p">(</span><span class="s1">'Received </span><span class="si">%r</span><span class="s1"> from </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">addr</span><span class="p">))</span>1059 <span class="nb">print</span><span class="p">(</span><span class="s1">'Send </span><span class="si">%r</span><span class="s1"> to </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">addr</span><span class="p">))</span>1060 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span><span class="o">.</span><span class="n">sendto</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">addr</span><span class="p">)</span>1061 1062 1063<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>1064 <span class="nb">print</span><span class="p">(</span><span class="s2">"Starting UDP server"</span><span class="p">)</span>1065 1066 <span class="c1"># Get a reference to the event loop as we plan to use</span>1067 <span class="c1"># low-level APIs.</span>1068 <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>1069 1070 <span class="c1"># One protocol instance will be created to serve all</span>1071 <span class="c1"># client requests.</span>1072 <span class="n">transport</span><span class="p">,</span> <span class="n">protocol</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_datagram_endpoint</span><span class="p">(</span>1073 <span class="n">EchoServerProtocol</span><span class="p">,</span>1074 <span class="n">local_addr</span><span class="o">=</span><span class="p">(</span><span class="s1">'127.0.0.1'</span><span class="p">,</span> <span class="mi">9999</span><span class="p">))</span>1075 1076 <span class="k">try</span><span class="p">:</span>1077 <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3600</span><span class="p">)</span> <span class="c1"># Serve for 1 hour.</span>1078 <span class="k">finally</span><span class="p">:</span>1079 <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>1080 1081 1082<span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>1083</pre></div>1084</div>1085</section>1086<section id="udp-echo-client">1087<span id="asyncio-udp-echo-client-protocol"></span><h3>UDP Echo Client<a class="headerlink" href="#udp-echo-client" title="Link to this heading">¶</a></h3>1088<p>A UDP echo client, using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_datagram_endpoint" title="asyncio.loop.create_datagram_endpoint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_datagram_endpoint()</span></code></a>1089method, sends data and closes the transport when it receives the answer:</p>1090<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>1091 1092 1093<span class="k">class</span><span class="w"> </span><span class="nc">EchoClientProtocol</span><span class="p">:</span>1094 <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">on_con_lost</span><span class="p">):</span>1095 <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>1096 <span class="bp">self</span><span class="o">.</span><span class="n">on_con_lost</span> <span class="o">=</span> <span class="n">on_con_lost</span>1097 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="kc">None</span>1098 1099 <span class="k">def</span><span class="w"> </span><span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transport</span><span class="p">):</span>1100 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>1101 <span class="nb">print</span><span class="p">(</span><span class="s1">'Send:'</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>1102 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span><span class="o">.</span><span class="n">sendto</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>1103 1104 <span class="k">def</span><span class="w"> </span><span class="nf">datagram_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">addr</span><span class="p">):</span>1105 <span class="nb">print</span><span class="p">(</span><span class="s2">"Received:"</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>1106 1107 <span class="nb">print</span><span class="p">(</span><span class="s2">"Close the socket"</span><span class="p">)</span>1108 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>1109 1110 <span class="k">def</span><span class="w"> </span><span class="nf">error_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>1111 <span class="nb">print</span><span class="p">(</span><span class="s1">'Error received:'</span><span class="p">,</span> <span class="n">exc</span><span class="p">)</span>1112 1113 <span class="k">def</span><span class="w"> </span><span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>1114 <span class="nb">print</span><span class="p">(</span><span class="s2">"Connection closed"</span><span class="p">)</span>1115 <span class="bp">self</span><span class="o">.</span><span class="n">on_con_lost</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>1116 1117 1118<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>1119 <span class="c1"># Get a reference to the event loop as we plan to use</span>1120 <span class="c1"># low-level APIs.</span>1121 <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>1122 1123 <span class="n">on_con_lost</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_future</span><span class="p">()</span>1124 <span class="n">message</span> <span class="o">=</span> <span class="s2">"Hello World!"</span>1125 1126 <span class="n">transport</span><span class="p">,</span> <span class="n">protocol</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_datagram_endpoint</span><span class="p">(</span>1127 <span class="k">lambda</span><span class="p">:</span> <span class="n">EchoClientProtocol</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">on_con_lost</span><span class="p">),</span>1128 <span class="n">remote_addr</span><span class="o">=</span><span class="p">(</span><span class="s1">'127.0.0.1'</span><span class="p">,</span> <span class="mi">9999</span><span class="p">))</span>1129 1130 <span class="k">try</span><span class="p">:</span>1131 <span class="k">await</span> <span class="n">on_con_lost</span>1132 <span class="k">finally</span><span class="p">:</span>1133 <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>1134 1135 1136<span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>1137</pre></div>1138</div>1139</section>1140<section id="connecting-existing-sockets">1141<span id="asyncio-example-create-connection"></span><h3>Connecting Existing Sockets<a class="headerlink" href="#connecting-existing-sockets" title="Link to this heading">¶</a></h3>1142<p>Wait until a socket receives data using the1143<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a> method with a protocol:</p>1144<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>1145<span class="kn">import</span><span class="w"> </span><span class="nn">socket</span>1146 1147 1148<span class="k">class</span><span class="w"> </span><span class="nc">MyProtocol</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">Protocol</span><span class="p">):</span>1149 1150 <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">on_con_lost</span><span class="p">):</span>1151 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="kc">None</span>1152 <span class="bp">self</span><span class="o">.</span><span class="n">on_con_lost</span> <span class="o">=</span> <span class="n">on_con_lost</span>1153 1154 <span class="k">def</span><span class="w"> </span><span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transport</span><span class="p">):</span>1155 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>1156 1157 <span class="k">def</span><span class="w"> </span><span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>1158 <span class="nb">print</span><span class="p">(</span><span class="s2">"Received:"</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>1159 1160 <span class="c1"># We are done: close the transport;</span>1161 <span class="c1"># connection_lost() will be called automatically.</span>1162 <span class="bp">self</span><span class="o">.</span><span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>1163 1164 <span class="k">def</span><span class="w"> </span><span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>1165 <span class="c1"># The socket has been closed</span>1166 <span class="bp">self</span><span class="o">.</span><span class="n">on_con_lost</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>1167 1168 1169<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>1170 <span class="c1"># Get a reference to the event loop as we plan to use</span>1171 <span class="c1"># low-level APIs.</span>1172 <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>1173 <span class="n">on_con_lost</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_future</span><span class="p">()</span>1174 1175 <span class="c1"># Create a pair of connected sockets</span>1176 <span class="n">rsock</span><span class="p">,</span> <span class="n">wsock</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socketpair</span><span class="p">()</span>1177 1178 <span class="c1"># Register the socket to wait for data.</span>1179 <span class="n">transport</span><span class="p">,</span> <span class="n">protocol</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span>1180 <span class="k">lambda</span><span class="p">:</span> <span class="n">MyProtocol</span><span class="p">(</span><span class="n">on_con_lost</span><span class="p">),</span> <span class="n">sock</span><span class="o">=</span><span class="n">rsock</span><span class="p">)</span>1181 1182 <span class="c1"># Simulate the reception of data from the network.</span>1183 <span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span><span class="n">wsock</span><span class="o">.</span><span class="n">send</span><span class="p">,</span> <span class="s1">'abc'</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>1184 1185 <span class="k">try</span><span class="p">:</span>1186 <span class="k">await</span> <span class="n">protocol</span><span class="o">.</span><span class="n">on_con_lost</span>1187 <span class="k">finally</span><span class="p">:</span>1188 <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>1189 <span class="n">wsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>1190 1191<span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>1192</pre></div>1193</div>1194<div class="admonition seealso">1195<p class="admonition-title">See also</p>1196<p>The <a class="reference internal" href="asyncio-eventloop.html#asyncio-example-watch-fd"><span class="std std-ref">watch a file descriptor for read events</span></a> example uses the low-level1197<a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.add_reader" title="asyncio.loop.add_reader"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.add_reader()</span></code></a> method to register an FD.</p>1198<p>The <a class="reference internal" href="asyncio-stream.html#asyncio-example-create-connection-streams"><span class="std std-ref">register an open socket to wait for data using streams</span></a> example uses high-level streams1199created by the <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">open_connection()</span></code></a> function in a coroutine.</p>1200</div>