gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1403 linhas
123KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Transports and Protocols" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/asyncio-protocol.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <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..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <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..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Transports and Protocols &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Policies" href="asyncio-policy.html" />
  33. <link rel="prev" title="Futures" href="asyncio-future.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/asyncio-protocol.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Transports and Protocols</a><ul>
  86. <li><a class="reference internal" href="#transports">Transports</a><ul>
  87. <li><a class="reference internal" href="#transports-hierarchy">Transports Hierarchy</a></li>
  88. <li><a class="reference internal" href="#base-transport">Base Transport</a></li>
  89. <li><a class="reference internal" href="#read-only-transports">Read-only Transports</a></li>
  90. <li><a class="reference internal" href="#write-only-transports">Write-only Transports</a></li>
  91. <li><a class="reference internal" href="#datagram-transports">Datagram Transports</a></li>
  92. <li><a class="reference internal" href="#subprocess-transports">Subprocess Transports</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#protocols">Protocols</a><ul>
  96. <li><a class="reference internal" href="#base-protocols">Base Protocols</a></li>
  97. <li><a class="reference internal" href="#base-protocol">Base Protocol</a></li>
  98. <li><a class="reference internal" href="#streaming-protocols">Streaming Protocols</a></li>
  99. <li><a class="reference internal" href="#buffered-streaming-protocols">Buffered Streaming Protocols</a></li>
  100. <li><a class="reference internal" href="#datagram-protocols">Datagram Protocols</a></li>
  101. <li><a class="reference internal" href="#subprocess-protocols">Subprocess Protocols</a></li>
  102. </ul>
  103. </li>
  104. <li><a class="reference internal" href="#examples">Examples</a><ul>
  105. <li><a class="reference internal" href="#tcp-echo-server">TCP Echo Server</a></li>
  106. <li><a class="reference internal" href="#tcp-echo-client">TCP Echo Client</a></li>
  107. <li><a class="reference internal" href="#udp-echo-server">UDP Echo Server</a></li>
  108. <li><a class="reference internal" href="#udp-echo-client">UDP Echo Client</a></li>
  109. <li><a class="reference internal" href="#connecting-existing-sockets">Connecting Existing Sockets</a></li>
  110. <li><a class="reference internal" href="#loop-subprocess-exec-and-subprocessprotocol">loop.subprocess_exec() and SubprocessProtocol</a></li>
  111. </ul>
  112. </li>
  113. </ul>
  114. </li>
  115. </ul>
  116. </div>
  117. <div>
  118. <h4>Previous topic</h4>
  119. <p class="topless"><a href="asyncio-future.html"
  120. title="previous chapter">Futures</a></p>
  121. </div>
  122. <div>
  123. <h4>Next topic</h4>
  124. <p class="topless"><a href="asyncio-policy.html"
  125. title="next chapter">Policies</a></p>
  126. </div>
  127. <div role="note" aria-label="source link">
  128. <h3>This Page</h3>
  129. <ul class="this-page-menu">
  130. <li><a href="../bugs.html">Report a Bug</a></li>
  131. <li>
  132. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-protocol.rst"
  133. rel="nofollow">Show Source
  134. </a>
  135. </li>
  136. </ul>
  137. </div>
  138. </nav>
  139. </div>
  140. </div>
  141. <div class="related" role="navigation" aria-label="related navigation">
  142. <h3>Navigation</h3>
  143. <ul>
  144. <li class="right" style="margin-right: 10px">
  145. <a href="../genindex.html" title="General Index"
  146. accesskey="I">index</a></li>
  147. <li class="right" >
  148. <a href="../py-modindex.html" title="Python Module Index"
  149. >modules</a> |</li>
  150. <li class="right" >
  151. <a href="asyncio-policy.html" title="Policies"
  152. accesskey="N">next</a> |</li>
  153. <li class="right" >
  154. <a href="asyncio-future.html" title="Futures"
  155. accesskey="P">previous</a> |</li>
  156. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  157. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  158. <li class="switchers">
  159. <div class="language_switcher_placeholder"></div>
  160. <div class="version_switcher_placeholder"></div>
  161. </li>
  162. <li>
  163. </li>
  164. <li id="cpython-language-and-version">
  165. <a href="../index.html">3.12.3 Documentation</a> &#187;
  166. </li>
  167. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  168. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  169. <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> &#187;</li>
  170. <li class="nav-item nav-item-this"><a href="">Transports and Protocols</a></li>
  171. <li class="right">
  172. <div class="inline-search" role="search">
  173. <form class="inline-search" action="../search.html" method="get">
  174. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  175. <input type="submit" value="Go" />
  176. </form>
  177. </div>
  178. |
  179. </li>
  180. <li class="right">
  181. <label class="theme-selector-label">
  182. Theme
  183. <select class="theme-selector" oninput="activateTheme(this.value)">
  184. <option value="auto" selected>Auto</option>
  185. <option value="light">Light</option>
  186. <option value="dark">Dark</option>
  187. </select>
  188. </label> |</li>
  189. </ul>
  190. </div>
  191. <div class="document">
  192. <div class="documentwrapper">
  193. <div class="bodywrapper">
  194. <div class="body" role="main">
  195. <section id="transports-and-protocols">
  196. <span id="asyncio-transports-protocols"></span><h1>Transports and Protocols<a class="headerlink" href="#transports-and-protocols" title="Link to this heading">¶</a></h1>
  197. <p class="rubric">Preface</p>
  198. <p>Transports and Protocols are used by the <strong>low-level</strong> event loop
  199. APIs 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 use
  200. callback-based programming style and enable high-performance
  201. implementations of network or IPC protocols (e.g. HTTP).</p>
  202. <p>Essentially, transports and protocols should only be used in
  203. libraries and frameworks and never in high-level asyncio
  204. applications.</p>
  205. <p>This documentation page covers both <a class="reference internal" href="#transports">Transports</a> and <a class="reference internal" href="#protocols">Protocols</a>.</p>
  206. <p class="rubric">Introduction</p>
  207. <p>At the highest level, the transport is concerned with <em>how</em> bytes
  208. are transmitted, while the protocol determines <em>which</em> bytes to
  209. transmit (and to some extent when).</p>
  210. <p>A different way of saying the same thing: a transport is an
  211. abstraction for a socket (or similar I/O endpoint) while a protocol
  212. is an abstraction for an application, from the transport’s point
  213. of view.</p>
  214. <p>Yet another view is the transport and protocol interfaces
  215. together define an abstract interface for using network I/O and
  216. interprocess I/O.</p>
  217. <p>There is always a 1:1 relationship between transport and protocol
  218. objects: the protocol calls transport methods to send data,
  219. while the transport calls protocol methods to pass it data that
  220. has been received.</p>
  221. <p>Most of connection oriented event loop methods
  222. (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 a
  223. <em>protocol_factory</em> argument used to create a <em>Protocol</em> object
  224. for an accepted connection, represented by a <em>Transport</em> object.
  225. Such methods usually return a tuple of <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>.</p>
  226. <p class="rubric">Contents</p>
  227. <p>This documentation page contains the following sections:</p>
  228. <ul class="simple">
  229. <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>,
  230. <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>,
  231. <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>
  232. classes.</p></li>
  233. <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>,
  234. <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>,
  235. <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>
  236. <li><p>The <a class="reference internal" href="#examples">Examples</a> section showcases how to work with transports,
  237. protocols, and low-level event loop APIs.</p></li>
  238. </ul>
  239. <section id="transports">
  240. <span id="asyncio-transport"></span><h2>Transports<a class="headerlink" href="#transports" title="Link to this heading">¶</a></h2>
  241. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/transports.py">Lib/asyncio/transports.py</a></p>
  242. <hr class="docutils" />
  243. <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 abstract
  244. various kinds of communication channels.</p>
  245. <p>Transport objects are always instantiated by an
  246. <a class="reference internal" href="asyncio-eventloop.html#asyncio-event-loop"><span class="std std-ref">asyncio event loop</span></a>.</p>
  247. <p>asyncio implements transports for TCP, UDP, SSL, and subprocess pipes.
  248. The methods available on a transport depend on the transport’s kind.</p>
  249. <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>
  250. <section id="transports-hierarchy">
  251. <h3>Transports Hierarchy<a class="headerlink" href="#transports-hierarchy" title="Link to this heading">¶</a></h3>
  252. <dl class="py class">
  253. <dt class="sig sig-object py" id="asyncio.BaseTransport">
  254. <em class="property"><span class="pre">class</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>
  255. <dd><p>Base class for all transports. Contains methods that all
  256. asyncio transports share.</p>
  257. </dd></dl>
  258. <dl class="py class">
  259. <dt class="sig sig-object py" id="asyncio.WriteTransport">
  260. <em class="property"><span class="pre">class</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>
  261. <dd><p>A base transport for write-only connections.</p>
  262. <p>Instances of the <em>WriteTransport</em> class are returned from
  263. the <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 and
  264. are also used by subprocess-related methods like
  265. <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>
  266. </dd></dl>
  267. <dl class="py class">
  268. <dt class="sig sig-object py" id="asyncio.ReadTransport">
  269. <em class="property"><span class="pre">class</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>
  270. <dd><p>A base transport for read-only connections.</p>
  271. <p>Instances of the <em>ReadTransport</em> class are returned from
  272. the <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 and
  273. are also used by subprocess-related methods like
  274. <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>
  275. </dd></dl>
  276. <dl class="py class">
  277. <dt class="sig sig-object py" id="asyncio.Transport">
  278. <em class="property"><span class="pre">class</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>
  279. <dd><p>Interface representing a bidirectional transport, such as a
  280. TCP connection.</p>
  281. <p>The user does not instantiate a transport directly; they call a
  282. utility function, passing it a protocol factory and other
  283. information necessary to create the transport and protocol.</p>
  284. <p>Instances of the <em>Transport</em> class are returned from or used by
  285. event 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>,
  286. <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>,
  287. <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>
  288. </dd></dl>
  289. <dl class="py class">
  290. <dt class="sig sig-object py" id="asyncio.DatagramTransport">
  291. <em class="property"><span class="pre">class</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>
  292. <dd><p>A transport for datagram (UDP) connections.</p>
  293. <p>Instances of the <em>DatagramTransport</em> class are returned from
  294. 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> event loop method.</p>
  295. </dd></dl>
  296. <dl class="py class">
  297. <dt class="sig sig-object py" id="asyncio.SubprocessTransport">
  298. <em class="property"><span class="pre">class</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>
  299. <dd><p>An abstraction to represent a connection between a parent and its
  300. child OS process.</p>
  301. <p>Instances of the <em>SubprocessTransport</em> class are returned from
  302. event 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> and
  303. <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>
  304. </dd></dl>
  305. </section>
  306. <section id="base-transport">
  307. <h3>Base Transport<a class="headerlink" href="#base-transport" title="Link to this heading">¶</a></h3>
  308. <dl class="py method">
  309. <dt class="sig sig-object py" id="asyncio.BaseTransport.close">
  310. <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>
  311. <dd><p>Close the transport.</p>
  312. <p>If the transport has a buffer for outgoing
  313. data, buffered data will be flushed asynchronously. No more data
  314. will be received. After all buffered data is flushed, the
  315. 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 be called with
  316. <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 be
  317. used once it is closed.</p>
  318. </dd></dl>
  319. <dl class="py method">
  320. <dt class="sig sig-object py" id="asyncio.BaseTransport.is_closing">
  321. <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>
  322. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the transport is closing or is closed.</p>
  323. </dd></dl>
  324. <dl class="py method">
  325. <dt class="sig sig-object py" id="asyncio.BaseTransport.get_extra_info">
  326. <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>
  327. <dd><p>Return information about the transport or underlying resources
  328. it uses.</p>
  329. <p><em>name</em> is a string representing the piece of transport-specific
  330. information to get.</p>
  331. <p><em>default</em> is the value to return if the information is not
  332. available, or if the transport does not support querying it
  333. with the given third-party event loop implementation or on the
  334. current platform.</p>
  335. <p>For example, the following code attempts to get the underlying
  336. socket object of the transport:</p>
  337. <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">&#39;socket&#39;</span><span class="p">)</span>
  338. <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>
  339. <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>
  340. </pre></div>
  341. </div>
  342. <p>Categories of information that can be queried on some transports:</p>
  343. <ul class="simple">
  344. <li><p>socket:</p>
  345. <ul>
  346. <li><p><code class="docutils literal notranslate"><span class="pre">'peername'</span></code>: the remote address to which the socket is
  347. connected, 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>
  348. (<code class="docutils literal notranslate"><span class="pre">None</span></code> on error)</p></li>
  349. <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>
  350. <li><p><code class="docutils literal notranslate"><span class="pre">'sockname'</span></code>: the socket’s own address,
  351. result 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>
  352. </ul>
  353. </li>
  354. <li><p>SSL socket:</p>
  355. <ul>
  356. <li><p><code class="docutils literal notranslate"><span class="pre">'compression'</span></code>: the compression algorithm being used as a
  357. string, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the connection isn’t compressed; result
  358. of <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>
  359. <li><p><code class="docutils literal notranslate"><span class="pre">'cipher'</span></code>: a three-value tuple containing the name of the
  360. cipher being used, the version of the SSL protocol that defines
  361. its use, and the number of secret bits being used; result of
  362. <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>
  363. <li><p><code class="docutils literal notranslate"><span class="pre">'peercert'</span></code>: peer certificate; result of
  364. <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>
  365. <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>
  366. <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> or
  367. <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>
  368. </ul>
  369. </li>
  370. <li><p>pipe:</p>
  371. <ul>
  372. <li><p><code class="docutils literal notranslate"><span class="pre">'pipe'</span></code>: pipe object</p></li>
  373. </ul>
  374. </li>
  375. <li><p>subprocess:</p>
  376. <ul>
  377. <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>
  378. </ul>
  379. </li>
  380. </ul>
  381. </dd></dl>
  382. <dl class="py method">
  383. <dt class="sig sig-object py" id="asyncio.BaseTransport.set_protocol">
  384. <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>
  385. <dd><p>Set a new protocol.</p>
  386. <p>Switching protocol should only be done when both
  387. protocols are documented to support the switch.</p>
  388. </dd></dl>
  389. <dl class="py method">
  390. <dt class="sig sig-object py" id="asyncio.BaseTransport.get_protocol">
  391. <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>
  392. <dd><p>Return the current protocol.</p>
  393. </dd></dl>
  394. </section>
  395. <section id="read-only-transports">
  396. <h3>Read-only Transports<a class="headerlink" href="#read-only-transports" title="Link to this heading">¶</a></h3>
  397. <dl class="py method">
  398. <dt class="sig sig-object py" id="asyncio.ReadTransport.is_reading">
  399. <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>
  400. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the transport is receiving new data.</p>
  401. <div class="versionadded">
  402. <p><span class="versionmodified added">New in version 3.7.</span></p>
  403. </div>
  404. </dd></dl>
  405. <dl class="py method">
  406. <dt class="sig sig-object py" id="asyncio.ReadTransport.pause_reading">
  407. <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>
  408. <dd><p>Pause the receiving end of the transport. No data will be passed to
  409. the 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>
  410. method 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>
  411. <div class="versionchanged">
  412. <p><span class="versionmodified changed">Changed in version 3.7: </span>The method is idempotent, i.e. it can be called when the
  413. transport is already paused or closed.</p>
  414. </div>
  415. </dd></dl>
  416. <dl class="py method">
  417. <dt class="sig sig-object py" id="asyncio.ReadTransport.resume_reading">
  418. <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>
  419. <dd><p>Resume the receiving end. The protocol’s
  420. <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> method
  421. will be called once again if some data is available for reading.</p>
  422. <div class="versionchanged">
  423. <p><span class="versionmodified changed">Changed in version 3.7: </span>The method is idempotent, i.e. it can be called when the
  424. transport is already reading.</p>
  425. </div>
  426. </dd></dl>
  427. </section>
  428. <section id="write-only-transports">
  429. <h3>Write-only Transports<a class="headerlink" href="#write-only-transports" title="Link to this heading">¶</a></h3>
  430. <dl class="py method">
  431. <dt class="sig sig-object py" id="asyncio.WriteTransport.abort">
  432. <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>
  433. <dd><p>Close the transport immediately, without waiting for pending operations
  434. to complete. Buffered data will be lost. No more data will be received.
  435. The 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 be
  436. 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>
  437. </dd></dl>
  438. <dl class="py method">
  439. <dt class="sig sig-object py" id="asyncio.WriteTransport.can_write_eof">
  440. <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>
  441. <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 supports
  442. <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>
  443. </dd></dl>
  444. <dl class="py method">
  445. <dt class="sig sig-object py" id="asyncio.WriteTransport.get_write_buffer_size">
  446. <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>
  447. <dd><p>Return the current size of the output buffer used by the transport.</p>
  448. </dd></dl>
  449. <dl class="py method">
  450. <dt class="sig sig-object py" id="asyncio.WriteTransport.get_write_buffer_limits">
  451. <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>
  452. <dd><p>Get the <em>high</em> and <em>low</em> watermarks for write flow control. Return a
  453. tuple <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 of
  454. bytes.</p>
  455. <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>
  456. <div class="versionadded">
  457. <p><span class="versionmodified added">New in version 3.4.2.</span></p>
  458. </div>
  459. </dd></dl>
  460. <dl class="py method">
  461. <dt class="sig sig-object py" id="asyncio.WriteTransport.set_write_buffer_limits">
  462. <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>
  463. <dd><p>Set the <em>high</em> and <em>low</em> watermarks for write flow control.</p>
  464. <p>These two values (measured in number of
  465. bytes) control when the protocol’s
  466. <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>
  467. and <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>
  468. methods are called. If specified, the low watermark must be less
  469. than or equal to the high watermark. Neither <em>high</em> nor <em>low</em>
  470. can be negative.</p>
  471. <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 size
  472. becomes greater than or equal to the <em>high</em> value. If writing has
  473. been 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 when
  474. the buffer size becomes less than or equal to the <em>low</em> value.</p>
  475. <p>The defaults are implementation-specific. If only the
  476. high watermark is given, the low watermark defaults to an
  477. implementation-specific value less than or equal to the
  478. high watermark. Setting <em>high</em> to zero forces <em>low</em> to zero as
  479. well, 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 called
  480. whenever the buffer becomes non-empty. Setting <em>low</em> to zero causes
  481. <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 the
  482. buffer is empty. Use of zero for either limit is generally
  483. sub-optimal as it reduces opportunities for doing I/O and
  484. computation concurrently.</p>
  485. <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>
  486. to get the limits.</p>
  487. </dd></dl>
  488. <dl class="py method">
  489. <dt class="sig sig-object py" id="asyncio.WriteTransport.write">
  490. <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>
  491. <dd><p>Write some <em>data</em> bytes to the transport.</p>
  492. <p>This method does not block; it buffers the data and arranges for it
  493. to be sent out asynchronously.</p>
  494. </dd></dl>
  495. <dl class="py method">
  496. <dt class="sig sig-object py" id="asyncio.WriteTransport.writelines">
  497. <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>
  498. <dd><p>Write a list (or any iterable) of data bytes to the transport.
  499. This 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 each
  500. element yielded by the iterable, but may be implemented more
  501. efficiently.</p>
  502. </dd></dl>
  503. <dl class="py method">
  504. <dt class="sig sig-object py" id="asyncio.WriteTransport.write_eof">
  505. <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>
  506. <dd><p>Close the write end of the transport after flushing all buffered data.
  507. Data may still be received.</p>
  508. <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 transport
  509. (e.g. SSL) doesn’t support half-closed connections.</p>
  510. </dd></dl>
  511. </section>
  512. <section id="datagram-transports">
  513. <h3>Datagram Transports<a class="headerlink" href="#datagram-transports" title="Link to this heading">¶</a></h3>
  514. <dl class="py method">
  515. <dt class="sig sig-object py" id="asyncio.DatagramTransport.sendto">
  516. <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>
  517. <dd><p>Send the <em>data</em> bytes to the remote peer given by <em>addr</em> (a
  518. transport-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>,
  519. the data is sent to the target address given on transport
  520. creation.</p>
  521. <p>This method does not block; it buffers the data and arranges
  522. for it to be sent out asynchronously.</p>
  523. </dd></dl>
  524. <dl class="py method">
  525. <dt class="sig sig-object py" id="asyncio.DatagramTransport.abort">
  526. <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>
  527. <dd><p>Close the transport immediately, without waiting for pending
  528. operations to complete. Buffered data will be lost.
  529. No more data will be received. The protocol’s
  530. <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>
  531. method 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>
  532. </dd></dl>
  533. </section>
  534. <section id="subprocess-transports">
  535. <span id="asyncio-subprocess-transports"></span><h3>Subprocess Transports<a class="headerlink" href="#subprocess-transports" title="Link to this heading">¶</a></h3>
  536. <dl class="py method">
  537. <dt class="sig sig-object py" id="asyncio.SubprocessTransport.get_pid">
  538. <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>
  539. <dd><p>Return the subprocess process id as an integer.</p>
  540. </dd></dl>
  541. <dl class="py method">
  542. <dt class="sig sig-object py" id="asyncio.SubprocessTransport.get_pipe_transport">
  543. <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>
  544. <dd><p>Return the transport for the communication pipe corresponding to the
  545. integer file descriptor <em>fd</em>:</p>
  546. <ul class="simple">
  547. <li><p><code class="docutils literal notranslate"><span class="pre">0</span></code>: readable streaming transport of the standard input (<em>stdin</em>),
  548. 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> if the subprocess was not created with <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code></p></li>
  549. <li><p><code class="docutils literal notranslate"><span class="pre">1</span></code>: writable streaming transport of the standard output (<em>stdout</em>),
  550. 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> if the subprocess was not created with <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code></p></li>
  551. <li><p><code class="docutils literal notranslate"><span class="pre">2</span></code>: writable streaming transport of the standard error (<em>stderr</em>),
  552. 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> if the subprocess was not created with <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code></p></li>
  553. <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>
  554. </ul>
  555. </dd></dl>
  556. <dl class="py method">
  557. <dt class="sig sig-object py" id="asyncio.SubprocessTransport.get_returncode">
  558. <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>
  559. <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>
  560. if it hasn’t returned, which is similar to the
  561. <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>
  562. </dd></dl>
  563. <dl class="py method">
  564. <dt class="sig sig-object py" id="asyncio.SubprocessTransport.kill">
  565. <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>
  566. <dd><p>Kill the subprocess.</p>
  567. <p>On POSIX systems, the function sends SIGKILL to the subprocess.
  568. On 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>
  569. <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>
  570. </dd></dl>
  571. <dl class="py method">
  572. <dt class="sig sig-object py" id="asyncio.SubprocessTransport.send_signal">
  573. <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>
  574. <dd><p>Send the <em>signal</em> number to the subprocess, as in
  575. <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>
  576. </dd></dl>
  577. <dl class="py method">
  578. <dt class="sig sig-object py" id="asyncio.SubprocessTransport.terminate">
  579. <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>
  580. <dd><p>Stop the subprocess.</p>
  581. <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.
  582. On Windows, the Windows API function <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> is called to
  583. stop the subprocess.</p>
  584. <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>
  585. </dd></dl>
  586. <dl class="py method">
  587. <dt class="sig sig-object py" id="asyncio.SubprocessTransport.close">
  588. <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>
  589. <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>
  590. <p>If the subprocess hasn’t returned yet, and close transports of
  591. <em>stdin</em>, <em>stdout</em>, and <em>stderr</em> pipes.</p>
  592. </dd></dl>
  593. </section>
  594. </section>
  595. <section id="protocols">
  596. <span id="asyncio-protocol"></span><h2>Protocols<a class="headerlink" href="#protocols" title="Link to this heading">¶</a></h2>
  597. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/protocols.py">Lib/asyncio/protocols.py</a></p>
  598. <hr class="docutils" />
  599. <p>asyncio provides a set of abstract base classes that should be used
  600. to implement network protocols. Those classes are meant to be used
  601. together with <a class="reference internal" href="#asyncio-transport"><span class="std std-ref">transports</span></a>.</p>
  602. <p>Subclasses of abstract base protocol classes may implement some or
  603. all methods. All these methods are callbacks: they are called by
  604. transports on certain events, for example when some data is received.
  605. A base protocol method should be called by the corresponding transport.</p>
  606. <section id="base-protocols">
  607. <h3>Base Protocols<a class="headerlink" href="#base-protocols" title="Link to this heading">¶</a></h3>
  608. <dl class="py class">
  609. <dt class="sig sig-object py" id="asyncio.BaseProtocol">
  610. <em class="property"><span class="pre">class</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>
  611. <dd><p>Base protocol with methods that all protocols share.</p>
  612. </dd></dl>
  613. <dl class="py class">
  614. <dt class="sig sig-object py" id="asyncio.Protocol">
  615. <em class="property"><span class="pre">class</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>
  616. <dd><p>The base class for implementing streaming protocols
  617. (TCP, Unix sockets, etc).</p>
  618. </dd></dl>
  619. <dl class="py class">
  620. <dt class="sig sig-object py" id="asyncio.BufferedProtocol">
  621. <em class="property"><span class="pre">class</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>
  622. <dd><p>A base class for implementing streaming protocols with manual
  623. control of the receive buffer.</p>
  624. </dd></dl>
  625. <dl class="py class">
  626. <dt class="sig sig-object py" id="asyncio.DatagramProtocol">
  627. <em class="property"><span class="pre">class</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>
  628. <dd><p>The base class for implementing datagram (UDP) protocols.</p>
  629. </dd></dl>
  630. <dl class="py class">
  631. <dt class="sig sig-object py" id="asyncio.SubprocessProtocol">
  632. <em class="property"><span class="pre">class</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>
  633. <dd><p>The base class for implementing protocols communicating with child
  634. processes (unidirectional pipes).</p>
  635. </dd></dl>
  636. </section>
  637. <section id="base-protocol">
  638. <h3>Base Protocol<a class="headerlink" href="#base-protocol" title="Link to this heading">¶</a></h3>
  639. <p>All asyncio protocols can implement Base Protocol callbacks.</p>
  640. <p class="rubric">Connection Callbacks</p>
  641. <p>Connection callbacks are called on all protocols, exactly once per
  642. a successful connection. All other protocol callbacks can only be
  643. called between those two methods.</p>
  644. <dl class="py method">
  645. <dt class="sig sig-object py" id="asyncio.BaseProtocol.connection_made">
  646. <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>
  647. <dd><p>Called when a connection is made.</p>
  648. <p>The <em>transport</em> argument is the transport representing the
  649. connection. The protocol is responsible for storing the reference
  650. to its transport.</p>
  651. </dd></dl>
  652. <dl class="py method">
  653. <dt class="sig sig-object py" id="asyncio.BaseProtocol.connection_lost">
  654. <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>
  655. <dd><p>Called when the connection is lost or closed.</p>
  656. <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>.
  657. The latter means a regular EOF is received, or the connection was
  658. aborted or closed by this side of the connection.</p>
  659. </dd></dl>
  660. <p class="rubric">Flow Control Callbacks</p>
  661. <p>Flow control callbacks can be called by transports to pause or
  662. resume writing performed by the protocol.</p>
  663. <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>
  664. method for more details.</p>
  665. <dl class="py method">
  666. <dt class="sig sig-object py" id="asyncio.BaseProtocol.pause_writing">
  667. <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>
  668. <dd><p>Called when the transport’s buffer goes over the high watermark.</p>
  669. </dd></dl>
  670. <dl class="py method">
  671. <dt class="sig sig-object py" id="asyncio.BaseProtocol.resume_writing">
  672. <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>
  673. <dd><p>Called when the transport’s buffer drains below the low watermark.</p>
  674. </dd></dl>
  675. <p>If the buffer size equals the high watermark,
  676. <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 must
  677. go strictly over.</p>
  678. <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 the
  679. buffer size is equal or lower than the low watermark. These end
  680. conditions are important to ensure that things go as expected when
  681. either mark is zero.</p>
  682. </section>
  683. <section id="streaming-protocols">
  684. <h3>Streaming Protocols<a class="headerlink" href="#streaming-protocols" title="Link to this heading">¶</a></h3>
  685. <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>,
  686. <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>,
  687. <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>,
  688. <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>
  689. accept factories that return streaming protocols.</p>
  690. <dl class="py method">
  691. <dt class="sig sig-object py" id="asyncio.Protocol.data_received">
  692. <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>
  693. <dd><p>Called when some data is received. <em>data</em> is a non-empty bytes
  694. object containing the incoming data.</p>
  695. <p>Whether the data is buffered, chunked or reassembled depends on
  696. the transport. In general, you shouldn’t rely on specific semantics
  697. and instead make your parsing generic and flexible. However,
  698. data is always received in the correct order.</p>
  699. <p>The method can be called an arbitrary number of times while
  700. a connection is open.</p>
  701. <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>
  702. is called at most once. Once <code class="docutils literal notranslate"><span class="pre">eof_received()</span></code> is called,
  703. <code class="docutils literal notranslate"><span class="pre">data_received()</span></code> is not called anymore.</p>
  704. </dd></dl>
  705. <dl class="py method">
  706. <dt class="sig sig-object py" id="asyncio.Protocol.eof_received">
  707. <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>
  708. <dd><p>Called when the other end signals it won’t send any more data
  709. (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 uses
  710. asyncio).</p>
  711. <p>This method may return a false value (including <code class="docutils literal notranslate"><span class="pre">None</span></code>), in which case
  712. the transport will close itself. Conversely, if this method returns a
  713. true value, the protocol used determines whether to close the transport.
  714. Since the default implementation returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, it implicitly closes the
  715. connection.</p>
  716. <p>Some transports, including SSL, don’t support half-closed connections,
  717. in which case returning true from this method will result in the connection
  718. being closed.</p>
  719. </dd></dl>
  720. <p>State machine:</p>
  721. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>start -&gt; connection_made
  722. [-&gt; data_received]*
  723. [-&gt; eof_received]?
  724. -&gt; connection_lost -&gt; end
  725. </pre></div>
  726. </div>
  727. </section>
  728. <section id="buffered-streaming-protocols">
  729. <h3>Buffered Streaming Protocols<a class="headerlink" href="#buffered-streaming-protocols" title="Link to this heading">¶</a></h3>
  730. <div class="versionadded">
  731. <p><span class="versionmodified added">New in version 3.7.</span></p>
  732. </div>
  733. <p>Buffered Protocols can be used with any event loop method
  734. that supports <a class="reference internal" href="#streaming-protocols">Streaming Protocols</a>.</p>
  735. <p><code class="docutils literal notranslate"><span class="pre">BufferedProtocol</span></code> implementations allow explicit manual allocation
  736. and control of the receive buffer. Event loops can then use the buffer
  737. provided by the protocol to avoid unnecessary data copies. This
  738. can result in noticeable performance improvement for protocols that
  739. receive big amounts of data. Sophisticated protocol implementations
  740. can significantly reduce the number of buffer allocations.</p>
  741. <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>
  742. instances:</p>
  743. <dl class="py method">
  744. <dt class="sig sig-object py" id="asyncio.BufferedProtocol.get_buffer">
  745. <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>
  746. <dd><p>Called to allocate a new receive buffer.</p>
  747. <p><em>sizehint</em> is the recommended minimum size for the returned
  748. buffer. It is acceptable to return smaller or larger buffers
  749. than what <em>sizehint</em> suggests. When set to -1, the buffer size
  750. can be arbitrary. It is an error to return a buffer with a zero size.</p>
  751. <p><code class="docutils literal notranslate"><span class="pre">get_buffer()</span></code> must return an object implementing the
  752. <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer protocol</span></a>.</p>
  753. </dd></dl>
  754. <dl class="py method">
  755. <dt class="sig sig-object py" id="asyncio.BufferedProtocol.buffer_updated">
  756. <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>
  757. <dd><p>Called when the buffer was updated with the received data.</p>
  758. <p><em>nbytes</em> is the total number of bytes that were written to the buffer.</p>
  759. </dd></dl>
  760. <dl class="py method">
  761. <dt class="sig sig-object py" id="asyncio.BufferedProtocol.eof_received">
  762. <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>
  763. <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>
  764. </dd></dl>
  765. <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 number
  766. of 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 once
  767. and, if called, <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> and
  768. <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>
  769. <p>State machine:</p>
  770. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>start -&gt; connection_made
  771. [-&gt; get_buffer
  772. [-&gt; buffer_updated]?
  773. ]*
  774. [-&gt; eof_received]?
  775. -&gt; connection_lost -&gt; end
  776. </pre></div>
  777. </div>
  778. </section>
  779. <section id="datagram-protocols">
  780. <h3>Datagram Protocols<a class="headerlink" href="#datagram-protocols" title="Link to this heading">¶</a></h3>
  781. <p>Datagram Protocol instances should be constructed by protocol
  782. factories 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>
  783. <dl class="py method">
  784. <dt class="sig sig-object py" id="asyncio.DatagramProtocol.datagram_received">
  785. <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>
  786. <dd><p>Called when a datagram is received. <em>data</em> is a bytes object containing
  787. the incoming data. <em>addr</em> is the address of the peer sending the data;
  788. the exact format depends on the transport.</p>
  789. </dd></dl>
  790. <dl class="py method">
  791. <dt class="sig sig-object py" id="asyncio.DatagramProtocol.error_received">
  792. <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>
  793. <dd><p>Called when a previous send or receive operation raises an
  794. <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 <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> instance.</p>
  795. <p>This method is called in rare conditions, when the transport (e.g. UDP)
  796. detects that a datagram could not be delivered to its recipient.
  797. In many conditions though, undeliverable datagrams will be silently
  798. dropped.</p>
  799. </dd></dl>
  800. <div class="admonition note">
  801. <p class="admonition-title">Note</p>
  802. <p>On BSD systems (macOS, FreeBSD, etc.) flow control is not supported
  803. for datagram protocols, because there is no reliable way to detect send
  804. failures caused by writing too many packets.</p>
  805. <p>The socket always appears ‘ready’ and excess packets are dropped. An
  806. <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> may
  807. or may not be raised; if it is raised, it will be reported to
  808. <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>
  809. </div>
  810. </section>
  811. <section id="subprocess-protocols">
  812. <span id="asyncio-subprocess-protocols"></span><h3>Subprocess Protocols<a class="headerlink" href="#subprocess-protocols" title="Link to this heading">¶</a></h3>
  813. <p>Subprocess Protocol instances should be constructed by protocol
  814. factories 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> and
  815. <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>
  816. <dl class="py method">
  817. <dt class="sig sig-object py" id="asyncio.SubprocessProtocol.pipe_data_received">
  818. <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>
  819. <dd><p>Called when the child process writes data into its stdout or stderr
  820. pipe.</p>
  821. <p><em>fd</em> is the integer file descriptor of the pipe.</p>
  822. <p><em>data</em> is a non-empty bytes object containing the received data.</p>
  823. </dd></dl>
  824. <dl class="py method">
  825. <dt class="sig sig-object py" id="asyncio.SubprocessProtocol.pipe_connection_lost">
  826. <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>
  827. <dd><p>Called when one of the pipes communicating with the child process
  828. is closed.</p>
  829. <p><em>fd</em> is the integer file descriptor that was closed.</p>
  830. </dd></dl>
  831. <dl class="py method">
  832. <dt class="sig sig-object py" id="asyncio.SubprocessProtocol.process_exited">
  833. <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>
  834. <dd><p>Called when the child process has exited.</p>
  835. <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> and
  836. <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>
  837. </dd></dl>
  838. </section>
  839. </section>
  840. <section id="examples">
  841. <h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  842. <section id="tcp-echo-server">
  843. <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>
  844. <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 back
  845. received data, and close the connection:</p>
  846. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  847. <span class="k">class</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>
  848. <span class="k">def</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>
  849. <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">&#39;peername&#39;</span><span class="p">)</span>
  850. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Connection from </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">peername</span><span class="p">))</span>
  851. <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
  852. <span class="k">def</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>
  853. <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>
  854. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Data received: </span><span class="si">{!r}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>
  855. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Send: </span><span class="si">{!r}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>
  856. <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>
  857. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Close the client socket&#39;</span><span class="p">)</span>
  858. <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>
  859. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  860. <span class="c1"># Get a reference to the event loop as we plan to use</span>
  861. <span class="c1"># low-level APIs.</span>
  862. <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>
  863. <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>
  864. <span class="k">lambda</span><span class="p">:</span> <span class="n">EchoServerProtocol</span><span class="p">(),</span>
  865. <span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">8888</span><span class="p">)</span>
  866. <span class="k">async</span> <span class="k">with</span> <span class="n">server</span><span class="p">:</span>
  867. <span class="k">await</span> <span class="n">server</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
  868. <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>
  869. </pre></div>
  870. </div>
  871. <div class="admonition seealso">
  872. <p class="admonition-title">See also</p>
  873. <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>
  874. example 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>
  875. </div>
  876. </section>
  877. <section id="tcp-echo-client">
  878. <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>
  879. <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, sends
  880. data, and waits until the connection is closed:</p>
  881. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  882. <span class="k">class</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>
  883. <span class="k">def</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>
  884. <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
  885. <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>
  886. <span class="k">def</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>
  887. <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>
  888. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Data sent: </span><span class="si">{!r}</span><span class="s1">&#39;</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>
  889. <span class="k">def</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>
  890. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Data received: </span><span class="si">{!r}</span><span class="s1">&#39;</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>
  891. <span class="k">def</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>
  892. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;The server closed the connection&#39;</span><span class="p">)</span>
  893. <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>
  894. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  895. <span class="c1"># Get a reference to the event loop as we plan to use</span>
  896. <span class="c1"># low-level APIs.</span>
  897. <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>
  898. <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>
  899. <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;Hello World!&#39;</span>
  900. <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>
  901. <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>
  902. <span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">8888</span><span class="p">)</span>
  903. <span class="c1"># Wait until the protocol signals that the connection</span>
  904. <span class="c1"># is lost and close the transport.</span>
  905. <span class="k">try</span><span class="p">:</span>
  906. <span class="k">await</span> <span class="n">on_con_lost</span>
  907. <span class="k">finally</span><span class="p">:</span>
  908. <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  909. <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>
  910. </pre></div>
  911. </div>
  912. <div class="admonition seealso">
  913. <p class="admonition-title">See also</p>
  914. <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>
  915. example 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>
  916. </div>
  917. </section>
  918. <section id="udp-echo-server">
  919. <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>
  920. <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>
  921. method, sends back received data:</p>
  922. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  923. <span class="k">class</span> <span class="nc">EchoServerProtocol</span><span class="p">:</span>
  924. <span class="k">def</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>
  925. <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
  926. <span class="k">def</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>
  927. <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>
  928. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Received </span><span class="si">%r</span><span class="s1"> from </span><span class="si">%s</span><span class="s1">&#39;</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>
  929. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Send </span><span class="si">%r</span><span class="s1"> to </span><span class="si">%s</span><span class="s1">&#39;</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>
  930. <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>
  931. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  932. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Starting UDP server&quot;</span><span class="p">)</span>
  933. <span class="c1"># Get a reference to the event loop as we plan to use</span>
  934. <span class="c1"># low-level APIs.</span>
  935. <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>
  936. <span class="c1"># One protocol instance will be created to serve all</span>
  937. <span class="c1"># client requests.</span>
  938. <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>
  939. <span class="k">lambda</span><span class="p">:</span> <span class="n">EchoServerProtocol</span><span class="p">(),</span>
  940. <span class="n">local_addr</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">9999</span><span class="p">))</span>
  941. <span class="k">try</span><span class="p">:</span>
  942. <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>
  943. <span class="k">finally</span><span class="p">:</span>
  944. <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  945. <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>
  946. </pre></div>
  947. </div>
  948. </section>
  949. <section id="udp-echo-client">
  950. <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>
  951. <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>
  952. method, sends data and closes the transport when it receives the answer:</p>
  953. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  954. <span class="k">class</span> <span class="nc">EchoClientProtocol</span><span class="p">:</span>
  955. <span class="k">def</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>
  956. <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
  957. <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>
  958. <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="kc">None</span>
  959. <span class="k">def</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>
  960. <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
  961. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Send:&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>
  962. <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>
  963. <span class="k">def</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>
  964. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Received:&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>
  965. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Close the socket&quot;</span><span class="p">)</span>
  966. <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>
  967. <span class="k">def</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>
  968. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Error received:&#39;</span><span class="p">,</span> <span class="n">exc</span><span class="p">)</span>
  969. <span class="k">def</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>
  970. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Connection closed&quot;</span><span class="p">)</span>
  971. <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>
  972. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  973. <span class="c1"># Get a reference to the event loop as we plan to use</span>
  974. <span class="c1"># low-level APIs.</span>
  975. <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>
  976. <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>
  977. <span class="n">message</span> <span class="o">=</span> <span class="s2">&quot;Hello World!&quot;</span>
  978. <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>
  979. <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>
  980. <span class="n">remote_addr</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">9999</span><span class="p">))</span>
  981. <span class="k">try</span><span class="p">:</span>
  982. <span class="k">await</span> <span class="n">on_con_lost</span>
  983. <span class="k">finally</span><span class="p">:</span>
  984. <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  985. <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>
  986. </pre></div>
  987. </div>
  988. </section>
  989. <section id="connecting-existing-sockets">
  990. <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>
  991. <p>Wait until a socket receives data using the
  992. <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>
  993. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  994. <span class="kn">import</span> <span class="nn">socket</span>
  995. <span class="k">class</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>
  996. <span class="k">def</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>
  997. <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="kc">None</span>
  998. <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>
  999. <span class="k">def</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>
  1000. <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
  1001. <span class="k">def</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>
  1002. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Received:&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>
  1003. <span class="c1"># We are done: close the transport;</span>
  1004. <span class="c1"># connection_lost() will be called automatically.</span>
  1005. <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>
  1006. <span class="k">def</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>
  1007. <span class="c1"># The socket has been closed</span>
  1008. <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>
  1009. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  1010. <span class="c1"># Get a reference to the event loop as we plan to use</span>
  1011. <span class="c1"># low-level APIs.</span>
  1012. <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>
  1013. <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>
  1014. <span class="c1"># Create a pair of connected sockets</span>
  1015. <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>
  1016. <span class="c1"># Register the socket to wait for data.</span>
  1017. <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>
  1018. <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>
  1019. <span class="c1"># Simulate the reception of data from the network.</span>
  1020. <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">&#39;abc&#39;</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>
  1021. <span class="k">try</span><span class="p">:</span>
  1022. <span class="k">await</span> <span class="n">protocol</span><span class="o">.</span><span class="n">on_con_lost</span>
  1023. <span class="k">finally</span><span class="p">:</span>
  1024. <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1025. <span class="n">wsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1026. <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>
  1027. </pre></div>
  1028. </div>
  1029. <div class="admonition seealso">
  1030. <p class="admonition-title">See also</p>
  1031. <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-level
  1032. <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>
  1033. <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 streams
  1034. created 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>
  1035. </div>
  1036. </section>
  1037. <section id="loop-subprocess-exec-and-subprocessprotocol">
  1038. <span id="asyncio-example-subprocess-proto"></span><h3>loop.subprocess_exec() and SubprocessProtocol<a class="headerlink" href="#loop-subprocess-exec-and-subprocessprotocol" title="Link to this heading">¶</a></h3>
  1039. <p>An example of a subprocess protocol used to get the output of a
  1040. subprocess and to wait for the subprocess exit.</p>
  1041. <p>The subprocess is created by 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> method:</p>
  1042. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  1043. <span class="kn">import</span> <span class="nn">sys</span>
  1044. <span class="k">class</span> <span class="nc">DateProtocol</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">SubprocessProtocol</span><span class="p">):</span>
  1045. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exit_future</span><span class="p">):</span>
  1046. <span class="bp">self</span><span class="o">.</span><span class="n">exit_future</span> <span class="o">=</span> <span class="n">exit_future</span>
  1047. <span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">()</span>
  1048. <span class="bp">self</span><span class="o">.</span><span class="n">pipe_closed</span> <span class="o">=</span> <span class="kc">False</span>
  1049. <span class="bp">self</span><span class="o">.</span><span class="n">exited</span> <span class="o">=</span> <span class="kc">False</span>
  1050. <span class="k">def</span> <span class="nf">pipe_connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
  1051. <span class="bp">self</span><span class="o">.</span><span class="n">pipe_closed</span> <span class="o">=</span> <span class="kc">True</span>
  1052. <span class="bp">self</span><span class="o">.</span><span class="n">check_for_exit</span><span class="p">()</span>
  1053. <span class="k">def</span> <span class="nf">pipe_data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  1054. <span class="bp">self</span><span class="o">.</span><span class="n">output</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  1055. <span class="k">def</span> <span class="nf">process_exited</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1056. <span class="bp">self</span><span class="o">.</span><span class="n">exited</span> <span class="o">=</span> <span class="kc">True</span>
  1057. <span class="c1"># process_exited() method can be called before</span>
  1058. <span class="c1"># pipe_connection_lost() method: wait until both methods are</span>
  1059. <span class="c1"># called.</span>
  1060. <span class="bp">self</span><span class="o">.</span><span class="n">check_for_exit</span><span class="p">()</span>
  1061. <span class="k">def</span> <span class="nf">check_for_exit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1062. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pipe_closed</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">exited</span><span class="p">:</span>
  1063. <span class="bp">self</span><span class="o">.</span><span class="n">exit_future</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
  1064. <span class="k">async</span> <span class="k">def</span> <span class="nf">get_date</span><span class="p">():</span>
  1065. <span class="c1"># Get a reference to the event loop as we plan to use</span>
  1066. <span class="c1"># low-level APIs.</span>
  1067. <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>
  1068. <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;import datetime; print(datetime.datetime.now())&#39;</span>
  1069. <span class="n">exit_future</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Future</span><span class="p">(</span><span class="n">loop</span><span class="o">=</span><span class="n">loop</span><span class="p">)</span>
  1070. <span class="c1"># Create the subprocess controlled by DateProtocol;</span>
  1071. <span class="c1"># redirect the standard output into a pipe.</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">subprocess_exec</span><span class="p">(</span>
  1073. <span class="k">lambda</span><span class="p">:</span> <span class="n">DateProtocol</span><span class="p">(</span><span class="n">exit_future</span><span class="p">),</span>
  1074. <span class="n">sys</span><span class="o">.</span><span class="n">executable</span><span class="p">,</span> <span class="s1">&#39;-c&#39;</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span>
  1075. <span class="n">stdin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
  1076. <span class="c1"># Wait for the subprocess exit using the process_exited()</span>
  1077. <span class="c1"># method of the protocol.</span>
  1078. <span class="k">await</span> <span class="n">exit_future</span>
  1079. <span class="c1"># Close the stdout pipe.</span>
  1080. <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1081. <span class="c1"># Read the output which was collected by the</span>
  1082. <span class="c1"># pipe_data_received() method of the protocol.</span>
  1083. <span class="n">data</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">protocol</span><span class="o">.</span><span class="n">output</span><span class="p">)</span>
  1084. <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
  1085. <span class="n">date</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">get_date</span><span class="p">())</span>
  1086. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Current date: </span><span class="si">{</span><span class="n">date</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
  1087. </pre></div>
  1088. </div>
  1089. <p>See also the <a class="reference internal" href="asyncio-subprocess.html#asyncio-example-create-subprocess-exec"><span class="std std-ref">same example</span></a>
  1090. written using high-level APIs.</p>
  1091. </section>
  1092. </section>
  1093. </section>
  1094. <div class="clearer"></div>
  1095. </div>
  1096. </div>
  1097. </div>
  1098. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1099. <div class="sphinxsidebarwrapper">
  1100. <div>
  1101. <h3><a href="../contents.html">Table of Contents</a></h3>
  1102. <ul>
  1103. <li><a class="reference internal" href="#">Transports and Protocols</a><ul>
  1104. <li><a class="reference internal" href="#transports">Transports</a><ul>
  1105. <li><a class="reference internal" href="#transports-hierarchy">Transports Hierarchy</a></li>
  1106. <li><a class="reference internal" href="#base-transport">Base Transport</a></li>
  1107. <li><a class="reference internal" href="#read-only-transports">Read-only Transports</a></li>
  1108. <li><a class="reference internal" href="#write-only-transports">Write-only Transports</a></li>
  1109. <li><a class="reference internal" href="#datagram-transports">Datagram Transports</a></li>
  1110. <li><a class="reference internal" href="#subprocess-transports">Subprocess Transports</a></li>
  1111. </ul>
  1112. </li>
  1113. <li><a class="reference internal" href="#protocols">Protocols</a><ul>
  1114. <li><a class="reference internal" href="#base-protocols">Base Protocols</a></li>
  1115. <li><a class="reference internal" href="#base-protocol">Base Protocol</a></li>
  1116. <li><a class="reference internal" href="#streaming-protocols">Streaming Protocols</a></li>
  1117. <li><a class="reference internal" href="#buffered-streaming-protocols">Buffered Streaming Protocols</a></li>
  1118. <li><a class="reference internal" href="#datagram-protocols">Datagram Protocols</a></li>
  1119. <li><a class="reference internal" href="#subprocess-protocols">Subprocess Protocols</a></li>
  1120. </ul>
  1121. </li>
  1122. <li><a class="reference internal" href="#examples">Examples</a><ul>
  1123. <li><a class="reference internal" href="#tcp-echo-server">TCP Echo Server</a></li>
  1124. <li><a class="reference internal" href="#tcp-echo-client">TCP Echo Client</a></li>
  1125. <li><a class="reference internal" href="#udp-echo-server">UDP Echo Server</a></li>
  1126. <li><a class="reference internal" href="#udp-echo-client">UDP Echo Client</a></li>
  1127. <li><a class="reference internal" href="#connecting-existing-sockets">Connecting Existing Sockets</a></li>
  1128. <li><a class="reference internal" href="#loop-subprocess-exec-and-subprocessprotocol">loop.subprocess_exec() and SubprocessProtocol</a></li>
  1129. </ul>
  1130. </li>
  1131. </ul>
  1132. </li>
  1133. </ul>
  1134. </div>
  1135. <div>
  1136. <h4>Previous topic</h4>
  1137. <p class="topless"><a href="asyncio-future.html"
  1138. title="previous chapter">Futures</a></p>
  1139. </div>
  1140. <div>
  1141. <h4>Next topic</h4>
  1142. <p class="topless"><a href="asyncio-policy.html"
  1143. title="next chapter">Policies</a></p>
  1144. </div>
  1145. <div role="note" aria-label="source link">
  1146. <h3>This Page</h3>
  1147. <ul class="this-page-menu">
  1148. <li><a href="../bugs.html">Report a Bug</a></li>
  1149. <li>
  1150. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-protocol.rst"
  1151. rel="nofollow">Show Source
  1152. </a>
  1153. </li>
  1154. </ul>
  1155. </div>
  1156. </div>
  1157. <div id="sidebarbutton" title="Collapse sidebar">
  1158. <span>«</span>
  1159. </div>
  1160. </div>
  1161. <div class="clearer"></div>
  1162. </div>
  1163. <div class="related" role="navigation" aria-label="related navigation">
  1164. <h3>Navigation</h3>
  1165. <ul>
  1166. <li class="right" style="margin-right: 10px">
  1167. <a href="../genindex.html" title="General Index"
  1168. >index</a></li>
  1169. <li class="right" >
  1170. <a href="../py-modindex.html" title="Python Module Index"
  1171. >modules</a> |</li>
  1172. <li class="right" >
  1173. <a href="asyncio-policy.html" title="Policies"
  1174. >next</a> |</li>
  1175. <li class="right" >
  1176. <a href="asyncio-future.html" title="Futures"
  1177. >previous</a> |</li>
  1178. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1179. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1180. <li class="switchers">
  1181. <div class="language_switcher_placeholder"></div>
  1182. <div class="version_switcher_placeholder"></div>
  1183. </li>
  1184. <li>
  1185. </li>
  1186. <li id="cpython-language-and-version">
  1187. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1188. </li>
  1189. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1190. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  1191. <li class="nav-item nav-item-3"><a href="asyncio.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> &#187;</li>
  1192. <li class="nav-item nav-item-this"><a href="">Transports and Protocols</a></li>
  1193. <li class="right">
  1194. <div class="inline-search" role="search">
  1195. <form class="inline-search" action="../search.html" method="get">
  1196. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1197. <input type="submit" value="Go" />
  1198. </form>
  1199. </div>
  1200. |
  1201. </li>
  1202. <li class="right">
  1203. <label class="theme-selector-label">
  1204. Theme
  1205. <select class="theme-selector" oninput="activateTheme(this.value)">
  1206. <option value="auto" selected>Auto</option>
  1207. <option value="light">Light</option>
  1208. <option value="dark">Dark</option>
  1209. </select>
  1210. </label> |</li>
  1211. </ul>
  1212. </div>
  1213. <div class="footer">
  1214. &copy;
  1215. <a href="../copyright.html">
  1216. Copyright
  1217. </a>
  1218. 2001-2024, Python Software Foundation.
  1219. <br />
  1220. This page is licensed under the Python Software Foundation License Version 2.
  1221. <br />
  1222. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1223. <br />
  1224. See <a href="/license.html">History and License</a> for more information.<br />
  1225. <br />
  1226. The Python Software Foundation is a non-profit corporation.
  1227. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1228. <br />
  1229. <br />
  1230. Last updated on Apr 09, 2024 (13:47 UTC).
  1231. <a href="/bugs.html">Found a bug</a>?
  1232. <br />
  1233. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1234. </div>
  1235. </body>
  1236. </html>
上海开阖软件有限公司 沪ICP备12045867号-1