~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/library/asyncio-protocol.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>18.5.4. Transports and protocols (callback based API) &mdash; Python 3.5.1 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '3.5.1',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 3.5.1 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
 
    <link rel="up" title="18.5. asyncio – Asynchronous I/O, event loop, coroutines and tasks" href="asyncio.html" />
34
 
    <link rel="next" title="18.5.5. Streams (coroutine based API)" href="asyncio-stream.html" />
35
 
    <link rel="prev" title="18.5.3. Tasks and coroutines" href="asyncio-task.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="asyncio-stream.html" title="18.5.5. Streams (coroutine based API)"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="asyncio-task.html" title="18.5.3. Tasks and coroutines"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="ipc.html" >18. Interprocess Communication and Networking</a> &raquo;</li>
69
 
          <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U">18.5. <code class="docutils literal"><span class="pre">asyncio</span></code> &#8211; Asynchronous I/O, event loop, coroutines and tasks</a> &raquo;</li> 
70
 
      </ul>
71
 
    </div>    
72
 
 
73
 
    <div class="document">
74
 
      <div class="documentwrapper">
75
 
        <div class="bodywrapper">
76
 
          <div class="body" role="main">
77
 
            
78
 
  <div class="section" id="transports-and-protocols-callback-based-api">
79
 
<h1>18.5.4. Transports  and protocols (callback based API)<a class="headerlink" href="#transports-and-protocols-callback-based-api" title="Permalink to this headline">¶</a></h1>
80
 
<div class="section" id="transports">
81
 
<span id="asyncio-transport"></span><h2>18.5.4.1. Transports<a class="headerlink" href="#transports" title="Permalink to this headline">¶</a></h2>
82
 
<p>Transports are classes provided by <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O, event loop, coroutines and tasks."><code class="xref py py-mod docutils literal"><span class="pre">asyncio</span></code></a> in order to abstract
83
 
various kinds of communication channels.  You generally won&#8217;t instantiate
84
 
a transport yourself; instead, you will call a <a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop" title="asyncio.BaseEventLoop"><code class="xref py py-class docutils literal"><span class="pre">BaseEventLoop</span></code></a> method
85
 
which will create the transport and try to initiate the underlying
86
 
communication channel, calling you back when it succeeds.</p>
87
 
<p>Once the communication channel is established, a transport is always
88
 
paired with a <a class="reference internal" href="#asyncio-protocol"><span>protocol</span></a> instance.  The protocol can
89
 
then call the transport&#8217;s methods for various purposes.</p>
90
 
<p><a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O, event loop, coroutines and tasks."><code class="xref py py-mod docutils literal"><span class="pre">asyncio</span></code></a> currently implements transports for TCP, UDP, SSL, and
91
 
subprocess pipes.  The methods available on a transport depend on
92
 
the transport&#8217;s kind.</p>
93
 
<p>The transport classes are <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span>not thread safe</span></a>.</p>
94
 
<div class="section" id="basetransport">
95
 
<h3>18.5.4.1.1. BaseTransport<a class="headerlink" href="#basetransport" title="Permalink to this headline">¶</a></h3>
96
 
<dl class="class">
97
 
<dt id="asyncio.BaseTransport">
98
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">BaseTransport</code><a class="headerlink" href="#asyncio.BaseTransport" title="Permalink to this definition">¶</a></dt>
99
 
<dd><p>Base class for transports.</p>
100
 
<dl class="method">
101
 
<dt id="asyncio.BaseTransport.close">
102
 
<code class="descname">close</code><span class="sig-paren">(</span><em>self</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.close" title="Permalink to this definition">¶</a></dt>
103
 
<dd><p>Close the transport.  If the transport has a buffer for outgoing
104
 
data, buffered data will be flushed asynchronously.  No more data
105
 
will be received.  After all buffered data is flushed, the
106
 
protocol&#8217;s <code class="xref py py-meth docutils literal"><span class="pre">connection_lost()</span></code> method will be called with
107
 
<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> as its argument.</p>
108
 
</dd></dl>
109
 
 
110
 
<dl class="method">
111
 
<dt id="asyncio.BaseTransport.is_closing">
112
 
<code class="descname">is_closing</code><span class="sig-paren">(</span><em>self</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.is_closing" title="Permalink to this definition">¶</a></dt>
113
 
<dd><p>Return <code class="docutils literal"><span class="pre">True</span></code> if the transport is closing or is closed.</p>
114
 
<div class="versionadded">
115
 
<p><span class="versionmodified">New in version 3.5.1.</span></p>
116
 
</div>
117
 
</dd></dl>
118
 
 
119
 
<dl class="method">
120
 
<dt id="asyncio.BaseTransport.get_extra_info">
121
 
<code class="descname">get_extra_info</code><span class="sig-paren">(</span><em>name</em>, <em>default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseTransport.get_extra_info" title="Permalink to this definition">¶</a></dt>
122
 
<dd><p>Return optional transport information.  <em>name</em> is a string representing
123
 
the piece of transport-specific information to get, <em>default</em> is the
124
 
value to return if the information doesn&#8217;t exist.</p>
125
 
<p>This method allows transport implementations to easily expose
126
 
channel-specific information.</p>
127
 
<ul class="simple">
128
 
<li>socket:<ul>
129
 
<li><code class="docutils literal"><span class="pre">'peername'</span></code>: the remote address to which the socket is connected,
130
 
result of <a class="reference internal" href="socket.html#socket.socket.getpeername" title="socket.socket.getpeername"><code class="xref py py-meth docutils literal"><span class="pre">socket.socket.getpeername()</span></code></a> (<code class="docutils literal"><span class="pre">None</span></code> on error)</li>
131
 
<li><code class="docutils literal"><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"><span class="pre">socket.socket</span></code></a> instance</li>
132
 
<li><code class="docutils literal"><span class="pre">'sockname'</span></code>: the socket&#8217;s own address,
133
 
result of <a class="reference internal" href="socket.html#socket.socket.getsockname" title="socket.socket.getsockname"><code class="xref py py-meth docutils literal"><span class="pre">socket.socket.getsockname()</span></code></a></li>
134
 
</ul>
135
 
</li>
136
 
<li>SSL socket:<ul>
137
 
<li><code class="docutils literal"><span class="pre">'compression'</span></code>: the compression algorithm being used as a string,
138
 
or <code class="docutils literal"><span class="pre">None</span></code> if the connection isn&#8217;t compressed; result of
139
 
<a class="reference internal" href="ssl.html#ssl.SSLSocket.compression" title="ssl.SSLSocket.compression"><code class="xref py py-meth docutils literal"><span class="pre">ssl.SSLSocket.compression()</span></code></a></li>
140
 
<li><code class="docutils literal"><span class="pre">'cipher'</span></code>: a three-value tuple containing the name of the cipher
141
 
being used, the version of the SSL protocol that defines its use, and
142
 
the number of secret bits being used; result of
143
 
<a class="reference internal" href="ssl.html#ssl.SSLSocket.cipher" title="ssl.SSLSocket.cipher"><code class="xref py py-meth docutils literal"><span class="pre">ssl.SSLSocket.cipher()</span></code></a></li>
144
 
<li><code class="docutils literal"><span class="pre">'peercert'</span></code>: peer certificate; result of
145
 
<a class="reference internal" href="ssl.html#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal"><span class="pre">ssl.SSLSocket.getpeercert()</span></code></a></li>
146
 
<li><code class="docutils literal"><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"><span class="pre">ssl.SSLContext</span></code></a> instance</li>
147
 
<li><code class="docutils literal"><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"><span class="pre">ssl.SSLObject</span></code></a> or <a class="reference internal" href="ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal"><span class="pre">ssl.SSLSocket</span></code></a>
148
 
instance</li>
149
 
</ul>
150
 
</li>
151
 
<li>pipe:<ul>
152
 
<li><code class="docutils literal"><span class="pre">'pipe'</span></code>: pipe object</li>
153
 
</ul>
154
 
</li>
155
 
<li>subprocess:<ul>
156
 
<li><code class="docutils literal"><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"><span class="pre">subprocess.Popen</span></code></a> instance</li>
157
 
</ul>
158
 
</li>
159
 
</ul>
160
 
</dd></dl>
161
 
 
162
 
<div class="versionchanged">
163
 
<p><span class="versionmodified">Changed in version 3.5.1: </span><code class="docutils literal"><span class="pre">'ssl_object'</span></code> info was added to SSL sockets.</p>
164
 
</div>
165
 
</dd></dl>
166
 
 
167
 
</div>
168
 
<div class="section" id="readtransport">
169
 
<h3>18.5.4.1.2. ReadTransport<a class="headerlink" href="#readtransport" title="Permalink to this headline">¶</a></h3>
170
 
<dl class="class">
171
 
<dt id="asyncio.ReadTransport">
172
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">ReadTransport</code><a class="headerlink" href="#asyncio.ReadTransport" title="Permalink to this definition">¶</a></dt>
173
 
<dd><p>Interface for read-only transports.</p>
174
 
<dl class="method">
175
 
<dt id="asyncio.ReadTransport.pause_reading">
176
 
<code class="descname">pause_reading</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.ReadTransport.pause_reading" title="Permalink to this definition">¶</a></dt>
177
 
<dd><p>Pause the receiving end of the transport.  No data will be passed to
178
 
the protocol&#8217;s <code class="xref py py-meth docutils literal"><span class="pre">data_received()</span></code> method until <a class="reference internal" href="#asyncio.ReadTransport.resume_reading" title="asyncio.ReadTransport.resume_reading"><code class="xref py py-meth docutils literal"><span class="pre">resume_reading()</span></code></a>
179
 
is called.</p>
180
 
</dd></dl>
181
 
 
182
 
<dl class="method">
183
 
<dt id="asyncio.ReadTransport.resume_reading">
184
 
<code class="descname">resume_reading</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.ReadTransport.resume_reading" title="Permalink to this definition">¶</a></dt>
185
 
<dd><p>Resume the receiving end.  The protocol&#8217;s <code class="xref py py-meth docutils literal"><span class="pre">data_received()</span></code> method
186
 
will be called once again if some data is available for reading.</p>
187
 
</dd></dl>
188
 
 
189
 
</dd></dl>
190
 
 
191
 
</div>
192
 
<div class="section" id="writetransport">
193
 
<h3>18.5.4.1.3. WriteTransport<a class="headerlink" href="#writetransport" title="Permalink to this headline">¶</a></h3>
194
 
<dl class="class">
195
 
<dt id="asyncio.WriteTransport">
196
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">WriteTransport</code><a class="headerlink" href="#asyncio.WriteTransport" title="Permalink to this definition">¶</a></dt>
197
 
<dd><p>Interface for write-only transports.</p>
198
 
<dl class="method">
199
 
<dt id="asyncio.WriteTransport.abort">
200
 
<code class="descname">abort</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.abort" title="Permalink to this definition">¶</a></dt>
201
 
<dd><p>Close the transport immediately, without waiting for pending operations
202
 
to complete.  Buffered data will be lost.  No more data will be received.
203
 
The protocol&#8217;s <code class="xref py py-meth docutils literal"><span class="pre">connection_lost()</span></code> method will eventually be
204
 
called with <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> as its argument.</p>
205
 
</dd></dl>
206
 
 
207
 
<dl class="method">
208
 
<dt id="asyncio.WriteTransport.can_write_eof">
209
 
<code class="descname">can_write_eof</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.can_write_eof" title="Permalink to this definition">¶</a></dt>
210
 
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the transport supports <a class="reference internal" href="#asyncio.WriteTransport.write_eof" title="asyncio.WriteTransport.write_eof"><code class="xref py py-meth docutils literal"><span class="pre">write_eof()</span></code></a>,
211
 
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> if not.</p>
212
 
</dd></dl>
213
 
 
214
 
<dl class="method">
215
 
<dt id="asyncio.WriteTransport.get_write_buffer_size">
216
 
<code class="descname">get_write_buffer_size</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.get_write_buffer_size" title="Permalink to this definition">¶</a></dt>
217
 
<dd><p>Return the current size of the output buffer used by the transport.</p>
218
 
</dd></dl>
219
 
 
220
 
<dl class="method">
221
 
<dt id="asyncio.WriteTransport.get_write_buffer_limits">
222
 
<code class="descname">get_write_buffer_limits</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.get_write_buffer_limits" title="Permalink to this definition">¶</a></dt>
223
 
<dd><p>Get the <em>high</em>- and <em>low</em>-water limits for write flow control. Return a
224
 
tuple <code class="docutils literal"><span class="pre">(low,</span> <span class="pre">high)</span></code> where <em>low</em> and <em>high</em> are positive number of
225
 
bytes.</p>
226
 
<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"><span class="pre">set_write_buffer_limits()</span></code></a> to set the limits.</p>
227
 
<div class="versionadded">
228
 
<p><span class="versionmodified">New in version 3.4.2.</span></p>
229
 
</div>
230
 
</dd></dl>
231
 
 
232
 
<dl class="method">
233
 
<dt id="asyncio.WriteTransport.set_write_buffer_limits">
234
 
<code class="descname">set_write_buffer_limits</code><span class="sig-paren">(</span><em>high=None</em>, <em>low=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.set_write_buffer_limits" title="Permalink to this definition">¶</a></dt>
235
 
<dd><p>Set the <em>high</em>- and <em>low</em>-water limits for write flow control.</p>
236
 
<p>These two values control when call the protocol&#8217;s
237
 
<code class="xref py py-meth docutils literal"><span class="pre">pause_writing()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">resume_writing()</span></code> methods are called.
238
 
If specified, the low-water limit must be less than or equal to the
239
 
high-water limit.  Neither <em>high</em> nor <em>low</em> can be negative.</p>
240
 
<p>The defaults are implementation-specific.  If only the
241
 
high-water limit is given, the low-water limit defaults to an
242
 
implementation-specific value less than or equal to the
243
 
high-water limit.  Setting <em>high</em> to zero forces <em>low</em> to zero as
244
 
well, and causes <code class="xref py py-meth docutils literal"><span class="pre">pause_writing()</span></code> to be called whenever the
245
 
buffer becomes non-empty.  Setting <em>low</em> to zero causes
246
 
<code class="xref py py-meth docutils literal"><span class="pre">resume_writing()</span></code> to be called only once the buffer is empty.
247
 
Use of zero for either limit is generally sub-optimal as it
248
 
reduces opportunities for doing I/O and computation
249
 
concurrently.</p>
250
 
<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"><span class="pre">get_write_buffer_limits()</span></code></a> to get the limits.</p>
251
 
</dd></dl>
252
 
 
253
 
<dl class="method">
254
 
<dt id="asyncio.WriteTransport.write">
255
 
<code class="descname">write</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.write" title="Permalink to this definition">¶</a></dt>
256
 
<dd><p>Write some <em>data</em> bytes to the transport.</p>
257
 
<p>This method does not block; it buffers the data and arranges for it
258
 
to be sent out asynchronously.</p>
259
 
</dd></dl>
260
 
 
261
 
<dl class="method">
262
 
<dt id="asyncio.WriteTransport.writelines">
263
 
<code class="descname">writelines</code><span class="sig-paren">(</span><em>list_of_data</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.writelines" title="Permalink to this definition">¶</a></dt>
264
 
<dd><p>Write a list (or any iterable) of data bytes to the transport.
265
 
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"><span class="pre">write()</span></code></a> on each
266
 
element yielded by the iterable, but may be implemented more efficiently.</p>
267
 
</dd></dl>
268
 
 
269
 
<dl class="method">
270
 
<dt id="asyncio.WriteTransport.write_eof">
271
 
<code class="descname">write_eof</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.WriteTransport.write_eof" title="Permalink to this definition">¶</a></dt>
272
 
<dd><p>Close the write end of the transport after flushing buffered data.
273
 
Data may still be received.</p>
274
 
<p>This method can raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a> if the transport
275
 
(e.g. SSL) doesn&#8217;t support half-closes.</p>
276
 
</dd></dl>
277
 
 
278
 
</dd></dl>
279
 
 
280
 
</div>
281
 
<div class="section" id="datagramtransport">
282
 
<h3>18.5.4.1.4. DatagramTransport<a class="headerlink" href="#datagramtransport" title="Permalink to this headline">¶</a></h3>
283
 
<dl class="method">
284
 
<dt id="asyncio.DatagramTransport.sendto">
285
 
<code class="descclassname">DatagramTransport.</code><code class="descname">sendto</code><span class="sig-paren">(</span><em>data</em>, <em>addr=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramTransport.sendto" title="Permalink to this definition">¶</a></dt>
286
 
<dd><p>Send the <em>data</em> bytes to the remote peer given by <em>addr</em> (a
287
 
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"><span class="pre">None</span></code></a>, the
288
 
data is sent to the target address given on transport creation.</p>
289
 
<p>This method does not block; it buffers the data and arranges for it
290
 
to be sent out asynchronously.</p>
291
 
</dd></dl>
292
 
 
293
 
<dl class="method">
294
 
<dt id="asyncio.DatagramTransport.abort">
295
 
<code class="descclassname">DatagramTransport.</code><code class="descname">abort</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramTransport.abort" title="Permalink to this definition">¶</a></dt>
296
 
<dd><p>Close the transport immediately, without waiting for pending operations
297
 
to complete.  Buffered data will be lost.  No more data will be received.
298
 
The protocol&#8217;s <code class="xref py py-meth docutils literal"><span class="pre">connection_lost()</span></code> method will eventually be
299
 
called with <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> as its argument.</p>
300
 
</dd></dl>
301
 
 
302
 
</div>
303
 
<div class="section" id="basesubprocesstransport">
304
 
<h3>18.5.4.1.5. BaseSubprocessTransport<a class="headerlink" href="#basesubprocesstransport" title="Permalink to this headline">¶</a></h3>
305
 
<dl class="class">
306
 
<dt id="asyncio.BaseSubprocessTransport">
307
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">BaseSubprocessTransport</code><a class="headerlink" href="#asyncio.BaseSubprocessTransport" title="Permalink to this definition">¶</a></dt>
308
 
<dd><dl class="method">
309
 
<dt id="asyncio.BaseSubprocessTransport.get_pid">
310
 
<code class="descname">get_pid</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseSubprocessTransport.get_pid" title="Permalink to this definition">¶</a></dt>
311
 
<dd><p>Return the subprocess process id as an integer.</p>
312
 
</dd></dl>
313
 
 
314
 
<dl class="method">
315
 
<dt id="asyncio.BaseSubprocessTransport.get_pipe_transport">
316
 
<code class="descname">get_pipe_transport</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseSubprocessTransport.get_pipe_transport" title="Permalink to this definition">¶</a></dt>
317
 
<dd><p>Return the transport for the communication pipe corresponding to the
318
 
integer file descriptor <em>fd</em>:</p>
319
 
<ul class="simple">
320
 
<li><code class="docutils literal"><span class="pre">0</span></code>: readable streaming transport of the standard input (<em>stdin</em>),
321
 
or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> if the subprocess was not created with <code class="docutils literal"><span class="pre">stdin=PIPE</span></code></li>
322
 
<li><code class="docutils literal"><span class="pre">1</span></code>: writable streaming transport of the standard output (<em>stdout</em>),
323
 
or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> if the subprocess was not created with <code class="docutils literal"><span class="pre">stdout=PIPE</span></code></li>
324
 
<li><code class="docutils literal"><span class="pre">2</span></code>: writable streaming transport of the standard error (<em>stderr</em>),
325
 
or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> if the subprocess was not created with <code class="docutils literal"><span class="pre">stderr=PIPE</span></code></li>
326
 
<li>other <em>fd</em>: <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a></li>
327
 
</ul>
328
 
</dd></dl>
329
 
 
330
 
<dl class="method">
331
 
<dt id="asyncio.BaseSubprocessTransport.get_returncode">
332
 
<code class="descname">get_returncode</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseSubprocessTransport.get_returncode" title="Permalink to this definition">¶</a></dt>
333
 
<dd><p>Return the subprocess returncode as an integer or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>
334
 
if it hasn&#8217;t returned, similarly to the
335
 
<a class="reference internal" href="subprocess.html#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><code class="xref py py-attr docutils literal"><span class="pre">subprocess.Popen.returncode</span></code></a> attribute.</p>
336
 
</dd></dl>
337
 
 
338
 
<dl class="method">
339
 
<dt id="asyncio.BaseSubprocessTransport.kill">
340
 
<code class="descname">kill</code><span class="sig-paren">(</span><em>self</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseSubprocessTransport.kill" title="Permalink to this definition">¶</a></dt>
341
 
<dd><p>Kill the subprocess, as in <a class="reference internal" href="subprocess.html#subprocess.Popen.kill" title="subprocess.Popen.kill"><code class="xref py py-meth docutils literal"><span class="pre">subprocess.Popen.kill()</span></code></a>.</p>
342
 
<p>On POSIX systems, the function sends SIGKILL to the subprocess.
343
 
On Windows, this method is an alias for <a class="reference internal" href="#asyncio.BaseSubprocessTransport.terminate" title="asyncio.BaseSubprocessTransport.terminate"><code class="xref py py-meth docutils literal"><span class="pre">terminate()</span></code></a>.</p>
344
 
</dd></dl>
345
 
 
346
 
<dl class="method">
347
 
<dt id="asyncio.BaseSubprocessTransport.send_signal">
348
 
<code class="descname">send_signal</code><span class="sig-paren">(</span><em>signal</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseSubprocessTransport.send_signal" title="Permalink to this definition">¶</a></dt>
349
 
<dd><p>Send the <em>signal</em> number to the subprocess, as in
350
 
<a class="reference internal" href="subprocess.html#subprocess.Popen.send_signal" title="subprocess.Popen.send_signal"><code class="xref py py-meth docutils literal"><span class="pre">subprocess.Popen.send_signal()</span></code></a>.</p>
351
 
</dd></dl>
352
 
 
353
 
<dl class="method">
354
 
<dt id="asyncio.BaseSubprocessTransport.terminate">
355
 
<code class="descname">terminate</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseSubprocessTransport.terminate" title="Permalink to this definition">¶</a></dt>
356
 
<dd><p>Ask the subprocess to stop, as in <a class="reference internal" href="subprocess.html#subprocess.Popen.terminate" title="subprocess.Popen.terminate"><code class="xref py py-meth docutils literal"><span class="pre">subprocess.Popen.terminate()</span></code></a>.
357
 
This method is an alias for the <a class="reference internal" href="#asyncio.BaseSubprocessTransport.close" title="asyncio.BaseSubprocessTransport.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a> method.</p>
358
 
<p>On POSIX systems, this method sends SIGTERM to the subprocess.
359
 
On Windows, the Windows API function TerminateProcess() is called to
360
 
stop the subprocess.</p>
361
 
</dd></dl>
362
 
 
363
 
<dl class="method">
364
 
<dt id="asyncio.BaseSubprocessTransport.close">
365
 
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseSubprocessTransport.close" title="Permalink to this definition">¶</a></dt>
366
 
<dd><p>Ask the subprocess to stop by calling the <a class="reference internal" href="#asyncio.BaseSubprocessTransport.terminate" title="asyncio.BaseSubprocessTransport.terminate"><code class="xref py py-meth docutils literal"><span class="pre">terminate()</span></code></a> method if the
367
 
subprocess hasn&#8217;t returned yet, and close transports of all pipes
368
 
(<em>stdin</em>, <em>stdout</em> and <em>stderr</em>).</p>
369
 
</dd></dl>
370
 
 
371
 
</dd></dl>
372
 
 
373
 
</div>
374
 
</div>
375
 
<div class="section" id="protocols">
376
 
<span id="asyncio-protocol"></span><h2>18.5.4.2. Protocols<a class="headerlink" href="#protocols" title="Permalink to this headline">¶</a></h2>
377
 
<p><a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O, event loop, coroutines and tasks."><code class="xref py py-mod docutils literal"><span class="pre">asyncio</span></code></a> provides base classes that you can subclass to implement
378
 
your network protocols.  Those classes are used in conjunction with
379
 
<a class="reference internal" href="#asyncio-transport"><span>transports</span></a> (see below): the protocol parses incoming
380
 
data and asks for the writing of outgoing data, while the transport is
381
 
responsible for the actual I/O and buffering.</p>
382
 
<p>When subclassing a protocol class, it is recommended you override certain
383
 
methods.  Those methods are callbacks: they will be called by the transport
384
 
on certain events (for example when some data is received); you shouldn&#8217;t
385
 
call them yourself, unless you are implementing a transport.</p>
386
 
<div class="admonition note">
387
 
<p class="first admonition-title">Note</p>
388
 
<p class="last">All callbacks have default implementations, which are empty.  Therefore,
389
 
you only need to implement the callbacks for the events in which you
390
 
are interested.</p>
391
 
</div>
392
 
<div class="section" id="protocol-classes">
393
 
<h3>18.5.4.2.1. Protocol classes<a class="headerlink" href="#protocol-classes" title="Permalink to this headline">¶</a></h3>
394
 
<dl class="class">
395
 
<dt id="asyncio.Protocol">
396
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">Protocol</code><a class="headerlink" href="#asyncio.Protocol" title="Permalink to this definition">¶</a></dt>
397
 
<dd><p>The base class for implementing streaming protocols (for use with
398
 
e.g. TCP and SSL transports).</p>
399
 
</dd></dl>
400
 
 
401
 
<dl class="class">
402
 
<dt id="asyncio.DatagramProtocol">
403
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">DatagramProtocol</code><a class="headerlink" href="#asyncio.DatagramProtocol" title="Permalink to this definition">¶</a></dt>
404
 
<dd><p>The base class for implementing datagram protocols (for use with
405
 
e.g. UDP transports).</p>
406
 
</dd></dl>
407
 
 
408
 
<dl class="class">
409
 
<dt id="asyncio.SubprocessProtocol">
410
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">SubprocessProtocol</code><a class="headerlink" href="#asyncio.SubprocessProtocol" title="Permalink to this definition">¶</a></dt>
411
 
<dd><p>The base class for implementing protocols communicating with child
412
 
processes (through a set of unidirectional pipes).</p>
413
 
</dd></dl>
414
 
 
415
 
</div>
416
 
<div class="section" id="connection-callbacks">
417
 
<h3>18.5.4.2.2. Connection callbacks<a class="headerlink" href="#connection-callbacks" title="Permalink to this headline">¶</a></h3>
418
 
<p>These callbacks may be called on <a class="reference internal" href="#asyncio.Protocol" title="asyncio.Protocol"><code class="xref py py-class docutils literal"><span class="pre">Protocol</span></code></a>, <a class="reference internal" href="#asyncio.DatagramProtocol" title="asyncio.DatagramProtocol"><code class="xref py py-class docutils literal"><span class="pre">DatagramProtocol</span></code></a>
419
 
and <a class="reference internal" href="#asyncio.SubprocessProtocol" title="asyncio.SubprocessProtocol"><code class="xref py py-class docutils literal"><span class="pre">SubprocessProtocol</span></code></a> instances:</p>
420
 
<dl class="method">
421
 
<dt id="asyncio.BaseProtocol.connection_made">
422
 
<code class="descclassname">BaseProtocol.</code><code class="descname">connection_made</code><span class="sig-paren">(</span><em>transport</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.connection_made" title="Permalink to this definition">¶</a></dt>
423
 
<dd><p>Called when a connection is made.</p>
424
 
<p>The <em>transport</em> argument is the transport representing the
425
 
connection.  You are responsible for storing it somewhere
426
 
(e.g. as an attribute) if you need to.</p>
427
 
</dd></dl>
428
 
 
429
 
<dl class="method">
430
 
<dt id="asyncio.BaseProtocol.connection_lost">
431
 
<code class="descclassname">BaseProtocol.</code><code class="descname">connection_lost</code><span class="sig-paren">(</span><em>exc</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.connection_lost" title="Permalink to this definition">¶</a></dt>
432
 
<dd><p>Called when the connection is lost or closed.</p>
433
 
<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"><span class="pre">None</span></code></a>.
434
 
The latter means a regular EOF is received, or the connection was
435
 
aborted or closed by this side of the connection.</p>
436
 
</dd></dl>
437
 
 
438
 
<p><a class="reference internal" href="#asyncio.BaseProtocol.connection_made" title="asyncio.BaseProtocol.connection_made"><code class="xref py py-meth docutils literal"><span class="pre">connection_made()</span></code></a> and <a class="reference internal" href="#asyncio.BaseProtocol.connection_lost" title="asyncio.BaseProtocol.connection_lost"><code class="xref py py-meth docutils literal"><span class="pre">connection_lost()</span></code></a>
439
 
are called exactly once per successful connection.  All other callbacks will be
440
 
called between those two methods, which allows for easier resource management
441
 
in your protocol implementation.</p>
442
 
<p>The following callbacks may be called only on <a class="reference internal" href="#asyncio.SubprocessProtocol" title="asyncio.SubprocessProtocol"><code class="xref py py-class docutils literal"><span class="pre">SubprocessProtocol</span></code></a>
443
 
instances:</p>
444
 
<dl class="method">
445
 
<dt id="asyncio.SubprocessProtocol.pipe_data_received">
446
 
<code class="descclassname">SubprocessProtocol.</code><code class="descname">pipe_data_received</code><span class="sig-paren">(</span><em>fd</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessProtocol.pipe_data_received" title="Permalink to this definition">¶</a></dt>
447
 
<dd><p>Called when the child process writes data into its stdout or stderr pipe.
448
 
<em>fd</em> is the integer file descriptor of the pipe.  <em>data</em> is a non-empty
449
 
bytes object containing the data.</p>
450
 
</dd></dl>
451
 
 
452
 
<dl class="method">
453
 
<dt id="asyncio.SubprocessProtocol.pipe_connection_lost">
454
 
<code class="descclassname">SubprocessProtocol.</code><code class="descname">pipe_connection_lost</code><span class="sig-paren">(</span><em>fd</em>, <em>exc</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessProtocol.pipe_connection_lost" title="Permalink to this definition">¶</a></dt>
455
 
<dd><p>Called when one of the pipes communicating with the child process
456
 
is closed.  <em>fd</em> is the integer file descriptor that was closed.</p>
457
 
</dd></dl>
458
 
 
459
 
<dl class="method">
460
 
<dt id="asyncio.SubprocessProtocol.process_exited">
461
 
<code class="descclassname">SubprocessProtocol.</code><code class="descname">process_exited</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.SubprocessProtocol.process_exited" title="Permalink to this definition">¶</a></dt>
462
 
<dd><p>Called when the child process has exited.</p>
463
 
</dd></dl>
464
 
 
465
 
</div>
466
 
<div class="section" id="streaming-protocols">
467
 
<h3>18.5.4.2.3. Streaming protocols<a class="headerlink" href="#streaming-protocols" title="Permalink to this headline">¶</a></h3>
468
 
<p>The following callbacks are called on <a class="reference internal" href="#asyncio.Protocol" title="asyncio.Protocol"><code class="xref py py-class docutils literal"><span class="pre">Protocol</span></code></a> instances:</p>
469
 
<dl class="method">
470
 
<dt id="asyncio.Protocol.data_received">
471
 
<code class="descclassname">Protocol.</code><code class="descname">data_received</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Protocol.data_received" title="Permalink to this definition">¶</a></dt>
472
 
<dd><p>Called when some data is received.  <em>data</em> is a non-empty bytes object
473
 
containing the incoming data.</p>
474
 
<div class="admonition note">
475
 
<p class="first admonition-title">Note</p>
476
 
<p class="last">Whether the data is buffered, chunked or reassembled depends on
477
 
the transport.  In general, you shouldn&#8217;t rely on specific semantics
478
 
and instead make your parsing generic and flexible enough.  However,
479
 
data is always received in the correct order.</p>
480
 
</div>
481
 
</dd></dl>
482
 
 
483
 
<dl class="method">
484
 
<dt id="asyncio.Protocol.eof_received">
485
 
<code class="descclassname">Protocol.</code><code class="descname">eof_received</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Protocol.eof_received" title="Permalink to this definition">¶</a></dt>
486
 
<dd><p>Calls when the other end signals it won&#8217;t send any more data
487
 
(for example by calling <code class="xref py py-meth docutils literal"><span class="pre">write_eof()</span></code>, if the other end also uses
488
 
asyncio).</p>
489
 
<p>This method may return a false value (including None), in which case
490
 
the transport will close itself.  Conversely, if this method returns a
491
 
true value, closing the transport is up to the protocol.  Since the
492
 
default implementation returns None, it implicitly closes the connection.</p>
493
 
<div class="admonition note">
494
 
<p class="first admonition-title">Note</p>
495
 
<p class="last">Some transports such as SSL don&#8217;t support half-closed connections,
496
 
in which case returning true from this method will not prevent closing
497
 
the connection.</p>
498
 
</div>
499
 
</dd></dl>
500
 
 
501
 
<p><code class="xref py py-meth docutils literal"><span class="pre">data_received()</span></code> can be called an arbitrary number of times during
502
 
a connection.  However, <code class="xref py py-meth docutils literal"><span class="pre">eof_received()</span></code> is called at most once
503
 
and, if called, <code class="xref py py-meth docutils literal"><span class="pre">data_received()</span></code> won&#8217;t be called after it.</p>
504
 
<p>State machine:</p>
505
 
<blockquote>
506
 
<div>start -&gt; <a class="reference internal" href="#asyncio.BaseProtocol.connection_made" title="asyncio.BaseProtocol.connection_made"><code class="xref py py-meth docutils literal"><span class="pre">connection_made()</span></code></a>
507
 
[-&gt; <a class="reference internal" href="#asyncio.Protocol.data_received" title="asyncio.Protocol.data_received"><code class="xref py py-meth docutils literal"><span class="pre">data_received()</span></code></a> *]
508
 
[-&gt; <a class="reference internal" href="#asyncio.Protocol.eof_received" title="asyncio.Protocol.eof_received"><code class="xref py py-meth docutils literal"><span class="pre">eof_received()</span></code></a> ?]
509
 
-&gt; <a class="reference internal" href="#asyncio.BaseProtocol.connection_lost" title="asyncio.BaseProtocol.connection_lost"><code class="xref py py-meth docutils literal"><span class="pre">connection_lost()</span></code></a> -&gt; end</div></blockquote>
510
 
</div>
511
 
<div class="section" id="datagram-protocols">
512
 
<h3>18.5.4.2.4. Datagram protocols<a class="headerlink" href="#datagram-protocols" title="Permalink to this headline">¶</a></h3>
513
 
<p>The following callbacks are called on <a class="reference internal" href="#asyncio.DatagramProtocol" title="asyncio.DatagramProtocol"><code class="xref py py-class docutils literal"><span class="pre">DatagramProtocol</span></code></a> instances.</p>
514
 
<dl class="method">
515
 
<dt id="asyncio.DatagramProtocol.datagram_received">
516
 
<code class="descclassname">DatagramProtocol.</code><code class="descname">datagram_received</code><span class="sig-paren">(</span><em>data</em>, <em>addr</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramProtocol.datagram_received" title="Permalink to this definition">¶</a></dt>
517
 
<dd><p>Called when a datagram is received.  <em>data</em> is a bytes object containing
518
 
the incoming data.  <em>addr</em> is the address of the peer sending the data;
519
 
the exact format depends on the transport.</p>
520
 
</dd></dl>
521
 
 
522
 
<dl class="method">
523
 
<dt id="asyncio.DatagramProtocol.error_received">
524
 
<code class="descclassname">DatagramProtocol.</code><code class="descname">error_received</code><span class="sig-paren">(</span><em>exc</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.DatagramProtocol.error_received" title="Permalink to this definition">¶</a></dt>
525
 
<dd><p>Called when a previous send or receive operation raises an
526
 
<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-class docutils literal"><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"><span class="pre">OSError</span></code></a> instance.</p>
527
 
<p>This method is called in rare conditions, when the transport (e.g. UDP)
528
 
detects that a datagram couldn&#8217;t be delivered to its recipient.
529
 
In many conditions though, undeliverable datagrams will be silently
530
 
dropped.</p>
531
 
</dd></dl>
532
 
 
533
 
</div>
534
 
<div class="section" id="flow-control-callbacks">
535
 
<h3>18.5.4.2.5. Flow control callbacks<a class="headerlink" href="#flow-control-callbacks" title="Permalink to this headline">¶</a></h3>
536
 
<p>These callbacks may be called on <a class="reference internal" href="#asyncio.Protocol" title="asyncio.Protocol"><code class="xref py py-class docutils literal"><span class="pre">Protocol</span></code></a>,
537
 
<a class="reference internal" href="#asyncio.DatagramProtocol" title="asyncio.DatagramProtocol"><code class="xref py py-class docutils literal"><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"><span class="pre">SubprocessProtocol</span></code></a> instances:</p>
538
 
<dl class="method">
539
 
<dt id="asyncio.BaseProtocol.pause_writing">
540
 
<code class="descclassname">BaseProtocol.</code><code class="descname">pause_writing</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.pause_writing" title="Permalink to this definition">¶</a></dt>
541
 
<dd><p>Called when the transport&#8217;s buffer goes over the high-water mark.</p>
542
 
</dd></dl>
543
 
 
544
 
<dl class="method">
545
 
<dt id="asyncio.BaseProtocol.resume_writing">
546
 
<code class="descclassname">BaseProtocol.</code><code class="descname">resume_writing</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseProtocol.resume_writing" title="Permalink to this definition">¶</a></dt>
547
 
<dd><p>Called when the transport&#8217;s buffer drains below the low-water mark.</p>
548
 
</dd></dl>
549
 
 
550
 
<p><code class="xref py py-meth docutils literal"><span class="pre">pause_writing()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">resume_writing()</span></code> calls are paired &#8211;
551
 
<code class="xref py py-meth docutils literal"><span class="pre">pause_writing()</span></code> is called once when the buffer goes strictly over
552
 
the high-water mark (even if subsequent writes increases the buffer size
553
 
even more), and eventually <code class="xref py py-meth docutils literal"><span class="pre">resume_writing()</span></code> is called once when the
554
 
buffer size reaches the low-water mark.</p>
555
 
<div class="admonition note">
556
 
<p class="first admonition-title">Note</p>
557
 
<p class="last">If the buffer size equals the high-water mark,
558
 
<code class="xref py py-meth docutils literal"><span class="pre">pause_writing()</span></code> is not called &#8211; it must go strictly over.
559
 
Conversely, <code class="xref py py-meth docutils literal"><span class="pre">resume_writing()</span></code> is called when the buffer size is
560
 
equal or lower than the low-water mark.  These end conditions
561
 
are important to ensure that things go as expected when either
562
 
mark is zero.</p>
563
 
</div>
564
 
<div class="admonition note">
565
 
<p class="first admonition-title">Note</p>
566
 
<p class="last">On BSD systems (OS X, FreeBSD, etc.) flow control is not supported
567
 
for <a class="reference internal" href="#asyncio.DatagramProtocol" title="asyncio.DatagramProtocol"><code class="xref py py-class docutils literal"><span class="pre">DatagramProtocol</span></code></a>, because send failures caused by
568
 
writing too many packets cannot be detected easily.  The socket
569
 
always appears &#8216;ready&#8217; and excess packets are dropped; an
570
 
<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-class docutils literal"><span class="pre">OSError</span></code></a> with errno set to <a class="reference internal" href="errno.html#errno.ENOBUFS" title="errno.ENOBUFS"><code class="xref py py-const docutils literal"><span class="pre">errno.ENOBUFS</span></code></a> may or
571
 
may not be raised; if it is raised, it will be reported to
572
 
<a class="reference internal" href="#asyncio.DatagramProtocol.error_received" title="asyncio.DatagramProtocol.error_received"><code class="xref py py-meth docutils literal"><span class="pre">DatagramProtocol.error_received()</span></code></a> but otherwise ignored.</p>
573
 
</div>
574
 
</div>
575
 
<div class="section" id="coroutines-and-protocols">
576
 
<h3>18.5.4.2.6. Coroutines and protocols<a class="headerlink" href="#coroutines-and-protocols" title="Permalink to this headline">¶</a></h3>
577
 
<p>Coroutines can be scheduled in a protocol method using <a class="reference internal" href="asyncio-task.html#asyncio.ensure_future" title="asyncio.ensure_future"><code class="xref py py-func docutils literal"><span class="pre">ensure_future()</span></code></a>,
578
 
but there is no guarantee made about the execution order.  Protocols are not
579
 
aware of coroutines created in protocol methods and so will not wait for them.</p>
580
 
<p>To have a reliable execution order, use <a class="reference internal" href="asyncio-stream.html#asyncio-streams"><span>stream objects</span></a> in a
581
 
coroutine with <code class="docutils literal"><span class="pre">yield</span> <span class="pre">from</span></code>. For example, the <a class="reference internal" href="asyncio-stream.html#asyncio.StreamWriter.drain" title="asyncio.StreamWriter.drain"><code class="xref py py-meth docutils literal"><span class="pre">StreamWriter.drain()</span></code></a>
582
 
coroutine can be used to wait until the write buffer is flushed.</p>
583
 
</div>
584
 
</div>
585
 
<div class="section" id="protocol-examples">
586
 
<h2>18.5.4.3. Protocol examples<a class="headerlink" href="#protocol-examples" title="Permalink to this headline">¶</a></h2>
587
 
<div class="section" id="tcp-echo-client-protocol">
588
 
<span id="asyncio-tcp-echo-client-protocol"></span><h3>18.5.4.3.1. TCP echo client protocol<a class="headerlink" href="#tcp-echo-client-protocol" title="Permalink to this headline">¶</a></h3>
589
 
<p>TCP echo client  using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.create_connection" title="asyncio.BaseEventLoop.create_connection"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_connection()</span></code></a> method, send
590
 
data and wait until the connection is closed:</p>
591
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
592
 
 
593
 
<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>
594
 
    <span class="k">def</span> <span class="nf">__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">loop</span><span class="p">):</span>
595
 
        <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
596
 
        <span class="bp">self</span><span class="o">.</span><span class="n">loop</span> <span class="o">=</span> <span class="n">loop</span>
597
 
 
598
 
    <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>
599
 
        <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>
600
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Data sent: {!r}&#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>
601
 
 
602
 
    <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>
603
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Data received: {!r}&#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>
604
 
 
605
 
    <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>
606
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;The server closed the connection&#39;</span><span class="p">)</span>
607
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Stop the event loop&#39;</span><span class="p">)</span>
608
 
        <span class="bp">self</span><span class="o">.</span><span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
609
 
 
610
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
611
 
<span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;Hello World!&#39;</span>
612
 
<span class="n">coro</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><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">loop</span><span class="p">),</span>
613
 
                              <span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">8888</span><span class="p">)</span>
614
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">coro</span><span class="p">)</span>
615
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
616
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
617
 
</pre></div>
618
 
</div>
619
 
<p>The event loop is running twice. The
620
 
<a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.run_until_complete" title="asyncio.BaseEventLoop.run_until_complete"><code class="xref py py-meth docutils literal"><span class="pre">run_until_complete()</span></code></a> method is preferred in this short
621
 
example to raise an exception if the server is not listening, instead of
622
 
having to write a short coroutine to handle the exception and stop the
623
 
running loop. At <a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.run_until_complete" title="asyncio.BaseEventLoop.run_until_complete"><code class="xref py py-meth docutils literal"><span class="pre">run_until_complete()</span></code></a> exit, the loop is
624
 
no longer running, so there is no need to stop the loop in case of an error.</p>
625
 
<div class="admonition seealso">
626
 
<p class="first admonition-title">See also</p>
627
 
<p class="last">The <a class="reference internal" href="asyncio-stream.html#asyncio-tcp-echo-client-streams"><span>TCP echo client using streams</span></a>
628
 
example uses the <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal"><span class="pre">asyncio.open_connection()</span></code></a> function.</p>
629
 
</div>
630
 
</div>
631
 
<div class="section" id="tcp-echo-server-protocol">
632
 
<span id="asyncio-tcp-echo-server-protocol"></span><h3>18.5.4.3.2. TCP echo server protocol<a class="headerlink" href="#tcp-echo-server-protocol" title="Permalink to this headline">¶</a></h3>
633
 
<p>TCP echo server using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.create_server" title="asyncio.BaseEventLoop.create_server"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_server()</span></code></a> method, send back
634
 
received data and close the connection:</p>
635
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
636
 
 
637
 
<span class="k">class</span> <span class="nc">EchoServerClientProtocol</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">Protocol</span><span class="p">):</span>
638
 
    <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>
639
 
        <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>
640
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Connection from {}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">peername</span><span class="p">))</span>
641
 
        <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
642
 
 
643
 
    <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>
644
 
        <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>
645
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Data received: {!r}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>
646
 
 
647
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Send: {!r}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>
648
 
        <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>
649
 
 
650
 
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Close the client socket&#39;</span><span class="p">)</span>
651
 
        <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>
652
 
 
653
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
654
 
<span class="c1"># Each client connection will create a new protocol instance</span>
655
 
<span class="n">coro</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">EchoServerClientProtocol</span><span class="p">,</span> <span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">8888</span><span class="p">)</span>
656
 
<span class="n">server</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">coro</span><span class="p">)</span>
657
 
 
658
 
<span class="c1"># Serve requests until Ctrl+C is pressed</span>
659
 
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Serving on {}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">server</span><span class="o">.</span><span class="n">sockets</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">getsockname</span><span class="p">()))</span>
660
 
<span class="k">try</span><span class="p">:</span>
661
 
    <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
662
 
<span class="k">except</span> <span class="ne">KeyboardInterrupt</span><span class="p">:</span>
663
 
    <span class="k">pass</span>
664
 
 
665
 
<span class="c1"># Close the server</span>
666
 
<span class="n">server</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
667
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">server</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">())</span>
668
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
669
 
</pre></div>
670
 
</div>
671
 
<p><code class="xref py py-meth docutils literal"><span class="pre">Transport.close()</span></code> can be called immediately after
672
 
<a class="reference internal" href="#asyncio.WriteTransport.write" title="asyncio.WriteTransport.write"><code class="xref py py-meth docutils literal"><span class="pre">WriteTransport.write()</span></code></a> even if data are not sent yet on the socket: both
673
 
methods are asynchronous. <code class="docutils literal"><span class="pre">yield</span> <span class="pre">from</span></code> is not needed because these transport
674
 
methods are not coroutines.</p>
675
 
<div class="admonition seealso">
676
 
<p class="first admonition-title">See also</p>
677
 
<p class="last">The <a class="reference internal" href="asyncio-stream.html#asyncio-tcp-echo-server-streams"><span>TCP echo server using streams</span></a>
678
 
example uses the <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal"><span class="pre">asyncio.start_server()</span></code></a> function.</p>
679
 
</div>
680
 
</div>
681
 
<div class="section" id="udp-echo-client-protocol">
682
 
<span id="asyncio-udp-echo-client-protocol"></span><h3>18.5.4.3.3. UDP echo client protocol<a class="headerlink" href="#udp-echo-client-protocol" title="Permalink to this headline">¶</a></h3>
683
 
<p>UDP echo client using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.create_datagram_endpoint" title="asyncio.BaseEventLoop.create_datagram_endpoint"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_datagram_endpoint()</span></code></a>
684
 
method, send data and close the transport when we received the answer:</p>
685
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
686
 
 
687
 
<span class="k">class</span> <span class="nc">EchoClientProtocol</span><span class="p">:</span>
688
 
    <span class="k">def</span> <span class="nf">__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">loop</span><span class="p">):</span>
689
 
        <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
690
 
        <span class="bp">self</span><span class="o">.</span><span class="n">loop</span> <span class="o">=</span> <span class="n">loop</span>
691
 
        <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="kc">None</span>
692
 
 
693
 
    <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>
694
 
        <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
695
 
        <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>
696
 
        <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>
697
 
 
698
 
    <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>
699
 
        <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>
700
 
 
701
 
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Close the socket&quot;</span><span class="p">)</span>
702
 
        <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>
703
 
 
704
 
    <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>
705
 
        <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>
706
 
 
707
 
    <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>
708
 
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Socket closed, stop the event loop&quot;</span><span class="p">)</span>
709
 
        <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
710
 
        <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
711
 
 
712
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
713
 
<span class="n">message</span> <span class="o">=</span> <span class="s2">&quot;Hello World!&quot;</span>
714
 
<span class="n">connect</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_datagram_endpoint</span><span class="p">(</span>
715
 
    <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">loop</span><span class="p">),</span>
716
 
    <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>
717
 
<span class="n">transport</span><span class="p">,</span> <span class="n">protocol</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">connect</span><span class="p">)</span>
718
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
719
 
<span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
720
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
721
 
</pre></div>
722
 
</div>
723
 
</div>
724
 
<div class="section" id="udp-echo-server-protocol">
725
 
<span id="asyncio-udp-echo-server-protocol"></span><h3>18.5.4.3.4. UDP echo server protocol<a class="headerlink" href="#udp-echo-server-protocol" title="Permalink to this headline">¶</a></h3>
726
 
<p>UDP echo server using the <a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.create_datagram_endpoint" title="asyncio.BaseEventLoop.create_datagram_endpoint"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_datagram_endpoint()</span></code></a>
727
 
method, send back received data:</p>
728
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
729
 
 
730
 
<span class="k">class</span> <span class="nc">EchoServerProtocol</span><span class="p">:</span>
731
 
    <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>
732
 
        <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
733
 
 
734
 
    <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>
735
 
        <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>
736
 
        <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>
737
 
        <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>
738
 
        <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>
739
 
 
740
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
741
 
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Starting UDP server&quot;</span><span class="p">)</span>
742
 
<span class="c1"># One protocol instance will be created to serve all client requests</span>
743
 
<span class="n">listen</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_datagram_endpoint</span><span class="p">(</span>
744
 
    <span class="n">EchoServerProtocol</span><span class="p">,</span> <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>
745
 
<span class="n">transport</span><span class="p">,</span> <span class="n">protocol</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">listen</span><span class="p">)</span>
746
 
 
747
 
<span class="k">try</span><span class="p">:</span>
748
 
    <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
749
 
<span class="k">except</span> <span class="ne">KeyboardInterrupt</span><span class="p">:</span>
750
 
    <span class="k">pass</span>
751
 
 
752
 
<span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
753
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
754
 
</pre></div>
755
 
</div>
756
 
</div>
757
 
<div class="section" id="register-an-open-socket-to-wait-for-data-using-a-protocol">
758
 
<span id="asyncio-register-socket"></span><h3>18.5.4.3.5. Register an open socket to wait for data using a protocol<a class="headerlink" href="#register-an-open-socket-to-wait-for-data-using-a-protocol" title="Permalink to this headline">¶</a></h3>
759
 
<p>Wait until a socket receives data using the
760
 
<a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.create_connection" title="asyncio.BaseEventLoop.create_connection"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_connection()</span></code></a> method with a protocol, and then close
761
 
the event loop</p>
762
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
763
 
<span class="k">try</span><span class="p">:</span>
764
 
    <span class="kn">from</span> <span class="nn">socket</span> <span class="k">import</span> <span class="n">socketpair</span>
765
 
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
766
 
    <span class="kn">from</span> <span class="nn">asyncio.windows_utils</span> <span class="k">import</span> <span class="n">socketpair</span>
767
 
 
768
 
<span class="c1"># Create a pair of connected sockets</span>
769
 
<span class="n">rsock</span><span class="p">,</span> <span class="n">wsock</span> <span class="o">=</span> <span class="n">socketpair</span><span class="p">()</span>
770
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
771
 
 
772
 
<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>
773
 
    <span class="n">transport</span> <span class="o">=</span> <span class="kc">None</span>
774
 
 
775
 
    <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>
776
 
        <span class="bp">self</span><span class="o">.</span><span class="n">transport</span> <span class="o">=</span> <span class="n">transport</span>
777
 
 
778
 
    <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>
779
 
        <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>
780
 
 
781
 
        <span class="c1"># We are done: close the transport (it will call connection_lost())</span>
782
 
        <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>
783
 
 
784
 
    <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>
785
 
        <span class="c1"># The socket has been closed, stop the event loop</span>
786
 
        <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
787
 
 
788
 
<span class="c1"># Register the socket to wait for data</span>
789
 
<span class="n">connect_coro</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><span class="n">MyProtocol</span><span class="p">,</span> <span class="n">sock</span><span class="o">=</span><span class="n">rsock</span><span class="p">)</span>
790
 
<span class="n">transport</span><span class="p">,</span> <span class="n">protocol</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">connect_coro</span><span class="p">)</span>
791
 
 
792
 
<span class="c1"># Simulate the reception of data from the network</span>
793
 
<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>
794
 
 
795
 
<span class="c1"># Run the event loop</span>
796
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
797
 
 
798
 
<span class="c1"># We are done, close sockets and the event loop</span>
799
 
<span class="n">rsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
800
 
<span class="n">wsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
801
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
802
 
</pre></div>
803
 
</div>
804
 
<div class="admonition seealso">
805
 
<p class="first admonition-title">See also</p>
806
 
<p>The <a class="reference internal" href="asyncio-eventloop.html#asyncio-watch-read-event"><span>watch a file descriptor for read events</span></a> example uses the low-level
807
 
<a class="reference internal" href="asyncio-eventloop.html#asyncio.BaseEventLoop.add_reader" title="asyncio.BaseEventLoop.add_reader"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.add_reader()</span></code></a> method to register the file descriptor of a
808
 
socket.</p>
809
 
<p class="last">The <a class="reference internal" href="asyncio-stream.html#asyncio-register-socket-streams"><span>register an open socket to wait for data using streams</span></a> example uses high-level streams
810
 
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"><span class="pre">open_connection()</span></code></a> function in a coroutine.</p>
811
 
</div>
812
 
</div>
813
 
</div>
814
 
</div>
815
 
 
816
 
 
817
 
          </div>
818
 
        </div>
819
 
      </div>
820
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
821
 
        <div class="sphinxsidebarwrapper">
822
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
823
 
  <ul>
824
 
<li><a class="reference internal" href="#">18.5.4. Transports  and protocols (callback based API)</a><ul>
825
 
<li><a class="reference internal" href="#transports">18.5.4.1. Transports</a><ul>
826
 
<li><a class="reference internal" href="#basetransport">18.5.4.1.1. BaseTransport</a></li>
827
 
<li><a class="reference internal" href="#readtransport">18.5.4.1.2. ReadTransport</a></li>
828
 
<li><a class="reference internal" href="#writetransport">18.5.4.1.3. WriteTransport</a></li>
829
 
<li><a class="reference internal" href="#datagramtransport">18.5.4.1.4. DatagramTransport</a></li>
830
 
<li><a class="reference internal" href="#basesubprocesstransport">18.5.4.1.5. BaseSubprocessTransport</a></li>
831
 
</ul>
832
 
</li>
833
 
<li><a class="reference internal" href="#protocols">18.5.4.2. Protocols</a><ul>
834
 
<li><a class="reference internal" href="#protocol-classes">18.5.4.2.1. Protocol classes</a></li>
835
 
<li><a class="reference internal" href="#connection-callbacks">18.5.4.2.2. Connection callbacks</a></li>
836
 
<li><a class="reference internal" href="#streaming-protocols">18.5.4.2.3. Streaming protocols</a></li>
837
 
<li><a class="reference internal" href="#datagram-protocols">18.5.4.2.4. Datagram protocols</a></li>
838
 
<li><a class="reference internal" href="#flow-control-callbacks">18.5.4.2.5. Flow control callbacks</a></li>
839
 
<li><a class="reference internal" href="#coroutines-and-protocols">18.5.4.2.6. Coroutines and protocols</a></li>
840
 
</ul>
841
 
</li>
842
 
<li><a class="reference internal" href="#protocol-examples">18.5.4.3. Protocol examples</a><ul>
843
 
<li><a class="reference internal" href="#tcp-echo-client-protocol">18.5.4.3.1. TCP echo client protocol</a></li>
844
 
<li><a class="reference internal" href="#tcp-echo-server-protocol">18.5.4.3.2. TCP echo server protocol</a></li>
845
 
<li><a class="reference internal" href="#udp-echo-client-protocol">18.5.4.3.3. UDP echo client protocol</a></li>
846
 
<li><a class="reference internal" href="#udp-echo-server-protocol">18.5.4.3.4. UDP echo server protocol</a></li>
847
 
<li><a class="reference internal" href="#register-an-open-socket-to-wait-for-data-using-a-protocol">18.5.4.3.5. Register an open socket to wait for data using a protocol</a></li>
848
 
</ul>
849
 
</li>
850
 
</ul>
851
 
</li>
852
 
</ul>
853
 
 
854
 
  <h4>Previous topic</h4>
855
 
  <p class="topless"><a href="asyncio-task.html"
856
 
                        title="previous chapter">18.5.3. Tasks and coroutines</a></p>
857
 
  <h4>Next topic</h4>
858
 
  <p class="topless"><a href="asyncio-stream.html"
859
 
                        title="next chapter">18.5.5. Streams (coroutine based API)</a></p>
860
 
<h3>This Page</h3>
861
 
<ul class="this-page-menu">
862
 
  <li><a href="../bugs.html">Report a Bug</a></li>
863
 
  <li><a href="../_sources/library/asyncio-protocol.txt"
864
 
         rel="nofollow">Show Source</a></li>
865
 
</ul>
866
 
 
867
 
<div id="searchbox" style="display: none" role="search">
868
 
  <h3>Quick search</h3>
869
 
    <form class="search" action="../search.html" method="get">
870
 
      <input type="text" name="q" />
871
 
      <input type="submit" value="Go" />
872
 
      <input type="hidden" name="check_keywords" value="yes" />
873
 
      <input type="hidden" name="area" value="default" />
874
 
    </form>
875
 
    <p class="searchtip" style="font-size: 90%">
876
 
    Enter search terms or a module, class or function name.
877
 
    </p>
878
 
</div>
879
 
<script type="text/javascript">$('#searchbox').show(0);</script>
880
 
        </div>
881
 
      </div>
882
 
      <div class="clearer"></div>
883
 
    </div>  
884
 
    <div class="related" role="navigation" aria-label="related navigation">
885
 
      <h3>Navigation</h3>
886
 
      <ul>
887
 
        <li class="right" style="margin-right: 10px">
888
 
          <a href="../genindex.html" title="General Index"
889
 
             >index</a></li>
890
 
        <li class="right" >
891
 
          <a href="../py-modindex.html" title="Python Module Index"
892
 
             >modules</a> |</li>
893
 
        <li class="right" >
894
 
          <a href="asyncio-stream.html" title="18.5.5. Streams (coroutine based API)"
895
 
             >next</a> |</li>
896
 
        <li class="right" >
897
 
          <a href="asyncio-task.html" title="18.5.3. Tasks and coroutines"
898
 
             >previous</a> |</li>
899
 
        <li><img src="../_static/py.png" alt=""
900
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
901
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
902
 
        <li>
903
 
          <span class="version_switcher_placeholder">3.5.1</span>
904
 
          <a href="../index.html">Documentation </a> &raquo;
905
 
        </li>
906
 
 
907
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
908
 
          <li class="nav-item nav-item-2"><a href="ipc.html" >18. Interprocess Communication and Networking</a> &raquo;</li>
909
 
          <li class="nav-item nav-item-3"><a href="asyncio.html" >18.5. <code class="docutils literal"><span class="pre">asyncio</span></code> &#8211; Asynchronous I/O, event loop, coroutines and tasks</a> &raquo;</li> 
910
 
      </ul>
911
 
    </div>  
912
 
    <div class="footer">
913
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
914
 
    <br />
915
 
    The Python Software Foundation is a non-profit corporation.
916
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
917
 
    <br />
918
 
    Last updated on Jan 22, 2016.
919
 
    <a href="../bugs.html">Found a bug</a>?
920
 
    <br />
921
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
922
 
    </div>
923
 
 
924
 
  </body>
925
 
</html>
 
 
b'\\ No newline at end of file'