~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.2-docs-html/library/asynchat.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.7. asynchat — Asynchronous socket command/response handler &mdash; Python 3.5.2 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.2',
 
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.2 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.2 documentation" href="../contents.html" />
 
33
    <link rel="up" title="18. Interprocess Communication and Networking" href="ipc.html" />
 
34
    <link rel="next" title="18.8. signal — Set handlers for asynchronous events" href="signal.html" />
 
35
    <link rel="prev" title="18.6. asyncore — Asynchronous socket handler" href="asyncore.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    
 
38
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
39
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
40
    
 
41
    
 
42
 
 
43
 
 
44
  </head>
 
45
  <body role="document">  
 
46
    <div class="related" role="navigation" aria-label="related navigation">
 
47
      <h3>Navigation</h3>
 
48
      <ul>
 
49
        <li class="right" style="margin-right: 10px">
 
50
          <a href="../genindex.html" title="General Index"
 
51
             accesskey="I">index</a></li>
 
52
        <li class="right" >
 
53
          <a href="../py-modindex.html" title="Python Module Index"
 
54
             >modules</a> |</li>
 
55
        <li class="right" >
 
56
          <a href="signal.html" title="18.8. signal — Set handlers for asynchronous events"
 
57
             accesskey="N">next</a> |</li>
 
58
        <li class="right" >
 
59
          <a href="asyncore.html" title="18.6. asyncore — Asynchronous socket handler"
 
60
             accesskey="P">previous</a> |</li>
 
61
        <li><img src="../_static/py.png" alt=""
 
62
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
63
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
64
        <li>
 
65
          <span class="version_switcher_placeholder">3.5.2</span>
 
66
          <a href="../index.html">Documentation </a> &raquo;
 
67
        </li>
 
68
 
 
69
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
70
          <li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">18. Interprocess Communication and Networking</a> &raquo;</li>
 
71
    <li class="right">
 
72
        
 
73
 
 
74
    <div class="inline-search" style="display: none" role="search">
 
75
        <form class="inline-search" action="../search.html" method="get">
 
76
          <input placeholder="Quick search" type="text" name="q" />
 
77
          <input type="submit" value="Go" />
 
78
          <input type="hidden" name="check_keywords" value="yes" />
 
79
          <input type="hidden" name="area" value="default" />
 
80
        </form>
 
81
    </div>
 
82
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
83
         |
 
84
    </li>
 
85
 
 
86
      </ul>
 
87
    </div>    
 
88
 
 
89
    <div class="document">
 
90
      <div class="documentwrapper">
 
91
        <div class="bodywrapper">
 
92
          <div class="body" role="main">
 
93
            
 
94
  <div class="section" id="module-asynchat">
 
95
<span id="asynchat-asynchronous-socket-command-response-handler"></span><h1>18.7. <a class="reference internal" href="#module-asynchat" title="asynchat: Support for asynchronous command/response protocols."><code class="xref py py-mod docutils literal"><span class="pre">asynchat</span></code></a> &#8212; Asynchronous socket command/response handler<a class="headerlink" href="#module-asynchat" title="Permalink to this headline">¶</a></h1>
 
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/asynchat.py">Lib/asynchat.py</a></p>
 
97
<hr class="docutils" />
 
98
<div class="admonition note">
 
99
<p class="first admonition-title">Note</p>
 
100
<p class="last">This module exists for backwards compatibility only.  For new code we
 
101
recommend using <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>.</p>
 
102
</div>
 
103
<p>This module builds on the <a class="reference internal" href="asyncore.html#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><code class="xref py py-mod docutils literal"><span class="pre">asyncore</span></code></a> infrastructure, simplifying
 
104
asynchronous clients and servers and making it easier to handle protocols
 
105
whose elements are terminated by arbitrary strings, or are of variable length.
 
106
<a class="reference internal" href="#module-asynchat" title="asynchat: Support for asynchronous command/response protocols."><code class="xref py py-mod docutils literal"><span class="pre">asynchat</span></code></a> defines the abstract class <a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a> that you
 
107
subclass, providing implementations of the <code class="xref py py-meth docutils literal"><span class="pre">collect_incoming_data()</span></code> and
 
108
<code class="xref py py-meth docutils literal"><span class="pre">found_terminator()</span></code> methods. It uses the same asynchronous loop as
 
109
<a class="reference internal" href="asyncore.html#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><code class="xref py py-mod docutils literal"><span class="pre">asyncore</span></code></a>, and the two types of channel, <a class="reference internal" href="asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></code></a>
 
110
and <a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">asynchat.async_chat</span></code></a>, can freely be mixed in the channel map.
 
111
Typically an <a class="reference internal" href="asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></code></a> server channel generates new
 
112
<a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">asynchat.async_chat</span></code></a> channel objects as it receives incoming
 
113
connection requests.</p>
 
114
<dl class="class">
 
115
<dt id="asynchat.async_chat">
 
116
<em class="property">class </em><code class="descclassname">asynchat.</code><code class="descname">async_chat</code><a class="headerlink" href="#asynchat.async_chat" title="Permalink to this definition">¶</a></dt>
 
117
<dd><p>This class is an abstract subclass of <a class="reference internal" href="asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></code></a>. To make
 
118
practical use of the code you must subclass <a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a>, providing
 
119
meaningful <a class="reference internal" href="#asynchat.async_chat.collect_incoming_data" title="asynchat.async_chat.collect_incoming_data"><code class="xref py py-meth docutils literal"><span class="pre">collect_incoming_data()</span></code></a> and <a class="reference internal" href="#asynchat.async_chat.found_terminator" title="asynchat.async_chat.found_terminator"><code class="xref py py-meth docutils literal"><span class="pre">found_terminator()</span></code></a>
 
120
methods.
 
121
The <a class="reference internal" href="asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></code></a> methods can be used, although not all make
 
122
sense in a message/response context.</p>
 
123
<p>Like <a class="reference internal" href="asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></code></a>, <a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a> defines a set of
 
124
events that are generated by an analysis of socket conditions after a
 
125
<code class="xref c c-func docutils literal"><span class="pre">select()</span></code> call. Once the polling loop has been started the
 
126
<a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a> object&#8217;s methods are called by the event-processing
 
127
framework with no action on the part of the programmer.</p>
 
128
<p>Two class attributes can be modified, to improve performance, or possibly
 
129
even to conserve memory.</p>
 
130
<dl class="data">
 
131
<dt id="asynchat.async_chat.ac_in_buffer_size">
 
132
<code class="descname">ac_in_buffer_size</code><a class="headerlink" href="#asynchat.async_chat.ac_in_buffer_size" title="Permalink to this definition">¶</a></dt>
 
133
<dd><p>The asynchronous input buffer size (default <code class="docutils literal"><span class="pre">4096</span></code>).</p>
 
134
</dd></dl>
 
135
 
 
136
<dl class="data">
 
137
<dt id="asynchat.async_chat.ac_out_buffer_size">
 
138
<code class="descname">ac_out_buffer_size</code><a class="headerlink" href="#asynchat.async_chat.ac_out_buffer_size" title="Permalink to this definition">¶</a></dt>
 
139
<dd><p>The asynchronous output buffer size (default <code class="docutils literal"><span class="pre">4096</span></code>).</p>
 
140
</dd></dl>
 
141
 
 
142
<p>Unlike <a class="reference internal" href="asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></code></a>, <a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a> allows you to
 
143
define a first-in-first-out queue (fifo) of <em>producers</em>. A producer need
 
144
have only one method, <code class="xref py py-meth docutils literal"><span class="pre">more()</span></code>, which should return data to be
 
145
transmitted on the channel.
 
146
The producer indicates exhaustion (<em>i.e.</em> that it contains no more data) by
 
147
having its <code class="xref py py-meth docutils literal"><span class="pre">more()</span></code> method return the empty bytes object. At this point
 
148
the <a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a> object removes the producer from the fifo and starts
 
149
using the next producer, if any. When the producer fifo is empty the
 
150
<code class="xref py py-meth docutils literal"><span class="pre">handle_write()</span></code> method does nothing. You use the channel object&#8217;s
 
151
<a class="reference internal" href="#asynchat.async_chat.set_terminator" title="asynchat.async_chat.set_terminator"><code class="xref py py-meth docutils literal"><span class="pre">set_terminator()</span></code></a> method to describe how to recognize the end of, or
 
152
an important breakpoint in, an incoming transmission from the remote
 
153
endpoint.</p>
 
154
<p>To build a functioning <a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a> subclass your  input methods
 
155
<a class="reference internal" href="#asynchat.async_chat.collect_incoming_data" title="asynchat.async_chat.collect_incoming_data"><code class="xref py py-meth docutils literal"><span class="pre">collect_incoming_data()</span></code></a> and <a class="reference internal" href="#asynchat.async_chat.found_terminator" title="asynchat.async_chat.found_terminator"><code class="xref py py-meth docutils literal"><span class="pre">found_terminator()</span></code></a> must handle the
 
156
data that the channel receives asynchronously. The methods are described
 
157
below.</p>
 
158
</dd></dl>
 
159
 
 
160
<dl class="method">
 
161
<dt id="asynchat.async_chat.close_when_done">
 
162
<code class="descclassname">async_chat.</code><code class="descname">close_when_done</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.close_when_done" title="Permalink to this definition">¶</a></dt>
 
163
<dd><p>Pushes a <code class="docutils literal"><span class="pre">None</span></code> on to the producer fifo. When this producer is popped off
 
164
the fifo it causes the channel to be closed.</p>
 
165
</dd></dl>
 
166
 
 
167
<dl class="method">
 
168
<dt id="asynchat.async_chat.collect_incoming_data">
 
169
<code class="descclassname">async_chat.</code><code class="descname">collect_incoming_data</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.collect_incoming_data" title="Permalink to this definition">¶</a></dt>
 
170
<dd><p>Called with <em>data</em> holding an arbitrary amount of received data.  The
 
171
default method, which must be overridden, raises a
 
172
<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> exception.</p>
 
173
</dd></dl>
 
174
 
 
175
<dl class="method">
 
176
<dt id="asynchat.async_chat.discard_buffers">
 
177
<code class="descclassname">async_chat.</code><code class="descname">discard_buffers</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.discard_buffers" title="Permalink to this definition">¶</a></dt>
 
178
<dd><p>In emergencies this method will discard any data held in the input and/or
 
179
output buffers and the producer fifo.</p>
 
180
</dd></dl>
 
181
 
 
182
<dl class="method">
 
183
<dt id="asynchat.async_chat.found_terminator">
 
184
<code class="descclassname">async_chat.</code><code class="descname">found_terminator</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.found_terminator" title="Permalink to this definition">¶</a></dt>
 
185
<dd><p>Called when the incoming data stream  matches the termination condition set
 
186
by <a class="reference internal" href="#asynchat.async_chat.set_terminator" title="asynchat.async_chat.set_terminator"><code class="xref py py-meth docutils literal"><span class="pre">set_terminator()</span></code></a>. The default method, which must be overridden,
 
187
raises a <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> exception. The buffered input data
 
188
should be available via an instance attribute.</p>
 
189
</dd></dl>
 
190
 
 
191
<dl class="method">
 
192
<dt id="asynchat.async_chat.get_terminator">
 
193
<code class="descclassname">async_chat.</code><code class="descname">get_terminator</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.get_terminator" title="Permalink to this definition">¶</a></dt>
 
194
<dd><p>Returns the current terminator for the channel.</p>
 
195
</dd></dl>
 
196
 
 
197
<dl class="method">
 
198
<dt id="asynchat.async_chat.push">
 
199
<code class="descclassname">async_chat.</code><code class="descname">push</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.push" title="Permalink to this definition">¶</a></dt>
 
200
<dd><p>Pushes data on to the channel&#8217;s fifo to ensure its transmission.
 
201
This is all you need to do to have the channel write the data out to the
 
202
network, although it is possible to use your own producers in more complex
 
203
schemes to implement encryption and chunking, for example.</p>
 
204
</dd></dl>
 
205
 
 
206
<dl class="method">
 
207
<dt id="asynchat.async_chat.push_with_producer">
 
208
<code class="descclassname">async_chat.</code><code class="descname">push_with_producer</code><span class="sig-paren">(</span><em>producer</em><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.push_with_producer" title="Permalink to this definition">¶</a></dt>
 
209
<dd><p>Takes a producer object and adds it to the producer fifo associated with
 
210
the channel.  When all currently-pushed producers have been exhausted the
 
211
channel will consume this producer&#8217;s data by calling its <code class="xref py py-meth docutils literal"><span class="pre">more()</span></code>
 
212
method and send the data to the remote endpoint.</p>
 
213
</dd></dl>
 
214
 
 
215
<dl class="method">
 
216
<dt id="asynchat.async_chat.set_terminator">
 
217
<code class="descclassname">async_chat.</code><code class="descname">set_terminator</code><span class="sig-paren">(</span><em>term</em><span class="sig-paren">)</span><a class="headerlink" href="#asynchat.async_chat.set_terminator" title="Permalink to this definition">¶</a></dt>
 
218
<dd><p>Sets the terminating condition to be recognized on the channel.  <code class="docutils literal"><span class="pre">term</span></code>
 
219
may be any of three types of value, corresponding to three different ways
 
220
to handle incoming protocol data.</p>
 
221
<table border="1" class="docutils">
 
222
<colgroup>
 
223
<col width="20%" />
 
224
<col width="80%" />
 
225
</colgroup>
 
226
<thead valign="bottom">
 
227
<tr class="row-odd"><th class="head">term</th>
 
228
<th class="head">Description</th>
 
229
</tr>
 
230
</thead>
 
231
<tbody valign="top">
 
232
<tr class="row-even"><td><em>string</em></td>
 
233
<td>Will call <a class="reference internal" href="#asynchat.async_chat.found_terminator" title="asynchat.async_chat.found_terminator"><code class="xref py py-meth docutils literal"><span class="pre">found_terminator()</span></code></a> when the
 
234
string is found in the input stream</td>
 
235
</tr>
 
236
<tr class="row-odd"><td><em>integer</em></td>
 
237
<td>Will call <a class="reference internal" href="#asynchat.async_chat.found_terminator" title="asynchat.async_chat.found_terminator"><code class="xref py py-meth docutils literal"><span class="pre">found_terminator()</span></code></a> when the
 
238
indicated number of characters have been
 
239
received</td>
 
240
</tr>
 
241
<tr class="row-even"><td><code class="docutils literal"><span class="pre">None</span></code></td>
 
242
<td>The channel continues to collect data
 
243
forever</td>
 
244
</tr>
 
245
</tbody>
 
246
</table>
 
247
<p>Note that any data following the terminator will be available for reading
 
248
by the channel after <a class="reference internal" href="#asynchat.async_chat.found_terminator" title="asynchat.async_chat.found_terminator"><code class="xref py py-meth docutils literal"><span class="pre">found_terminator()</span></code></a> is called.</p>
 
249
</dd></dl>
 
250
 
 
251
<div class="section" id="asynchat-example">
 
252
<span id="id1"></span><h2>18.7.1. asynchat Example<a class="headerlink" href="#asynchat-example" title="Permalink to this headline">¶</a></h2>
 
253
<p>The following partial example shows how HTTP requests can be read with
 
254
<a class="reference internal" href="#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal"><span class="pre">async_chat</span></code></a>.  A web server might create an
 
255
<code class="xref py py-class docutils literal"><span class="pre">http_request_handler</span></code> object for each incoming client connection.
 
256
Notice that initially the channel terminator is set to match the blank line at
 
257
the end of the HTTP headers, and a flag indicates that the headers are being
 
258
read.</p>
 
259
<p>Once the headers have been read, if the request is of type POST (indicating
 
260
that further data are present in the input stream) then the
 
261
<code class="docutils literal"><span class="pre">Content-Length:</span></code> header is used to set a numeric terminator to read the
 
262
right amount of data from the channel.</p>
 
263
<p>The <code class="xref py py-meth docutils literal"><span class="pre">handle_request()</span></code> method is called once all relevant input has been
 
264
marshalled, after setting the channel terminator to <code class="docutils literal"><span class="pre">None</span></code> to ensure that
 
265
any extraneous data sent by the web client are ignored.</p>
 
266
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asynchat</span>
 
267
 
 
268
<span class="k">class</span> <span class="nc">http_request_handler</span><span class="p">(</span><span class="n">asynchat</span><span class="o">.</span><span class="n">async_chat</span><span class="p">):</span>
 
269
 
 
270
    <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">sock</span><span class="p">,</span> <span class="n">addr</span><span class="p">,</span> <span class="n">sessions</span><span class="p">,</span> <span class="n">log</span><span class="p">):</span>
 
271
        <span class="n">asynchat</span><span class="o">.</span><span class="n">async_chat</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="o">=</span><span class="n">sock</span><span class="p">)</span>
 
272
        <span class="bp">self</span><span class="o">.</span><span class="n">addr</span> <span class="o">=</span> <span class="n">addr</span>
 
273
        <span class="bp">self</span><span class="o">.</span><span class="n">sessions</span> <span class="o">=</span> <span class="n">sessions</span>
 
274
        <span class="bp">self</span><span class="o">.</span><span class="n">ibuffer</span> <span class="o">=</span> <span class="p">[]</span>
 
275
        <span class="bp">self</span><span class="o">.</span><span class="n">obuffer</span> <span class="o">=</span> <span class="n">b</span><span class="s2">&quot;&quot;</span>
 
276
        <span class="bp">self</span><span class="o">.</span><span class="n">set_terminator</span><span class="p">(</span><span class="n">b</span><span class="s2">&quot;</span><span class="se">\r\n\r\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
277
        <span class="bp">self</span><span class="o">.</span><span class="n">reading_headers</span> <span class="o">=</span> <span class="kc">True</span>
 
278
        <span class="bp">self</span><span class="o">.</span><span class="n">handling</span> <span class="o">=</span> <span class="kc">False</span>
 
279
        <span class="bp">self</span><span class="o">.</span><span class="n">cgi_data</span> <span class="o">=</span> <span class="kc">None</span>
 
280
        <span class="bp">self</span><span class="o">.</span><span class="n">log</span> <span class="o">=</span> <span class="n">log</span>
 
281
 
 
282
    <span class="k">def</span> <span class="nf">collect_incoming_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
 
283
        <span class="sd">&quot;&quot;&quot;Buffer the data&quot;&quot;&quot;</span>
 
284
        <span class="bp">self</span><span class="o">.</span><span class="n">ibuffer</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
 
285
 
 
286
    <span class="k">def</span> <span class="nf">found_terminator</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 
287
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reading_headers</span><span class="p">:</span>
 
288
            <span class="bp">self</span><span class="o">.</span><span class="n">reading_headers</span> <span class="o">=</span> <span class="kc">False</span>
 
289
            <span class="bp">self</span><span class="o">.</span><span class="n">parse_headers</span><span class="p">(</span><span class="n">b</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ibuffer</span><span class="p">))</span>
 
290
            <span class="bp">self</span><span class="o">.</span><span class="n">ibuffer</span> <span class="o">=</span> <span class="p">[]</span>
 
291
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">op</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="o">==</span> <span class="n">b</span><span class="s2">&quot;POST&quot;</span><span class="p">:</span>
 
292
                <span class="n">clen</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="s2">&quot;content-length&quot;</span><span class="p">)</span>
 
293
                <span class="bp">self</span><span class="o">.</span><span class="n">set_terminator</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">clen</span><span class="p">))</span>
 
294
            <span class="k">else</span><span class="p">:</span>
 
295
                <span class="bp">self</span><span class="o">.</span><span class="n">handling</span> <span class="o">=</span> <span class="kc">True</span>
 
296
                <span class="bp">self</span><span class="o">.</span><span class="n">set_terminator</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
 
297
                <span class="bp">self</span><span class="o">.</span><span class="n">handle_request</span><span class="p">()</span>
 
298
        <span class="k">elif</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">handling</span><span class="p">:</span>
 
299
            <span class="bp">self</span><span class="o">.</span><span class="n">set_terminator</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>  <span class="c1"># browsers sometimes over-send</span>
 
300
            <span class="bp">self</span><span class="o">.</span><span class="n">cgi_data</span> <span class="o">=</span> <span class="n">parse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">headers</span><span class="p">,</span> <span class="n">b</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ibuffer</span><span class="p">))</span>
 
301
            <span class="bp">self</span><span class="o">.</span><span class="n">handling</span> <span class="o">=</span> <span class="kc">True</span>
 
302
            <span class="bp">self</span><span class="o">.</span><span class="n">ibuffer</span> <span class="o">=</span> <span class="p">[]</span>
 
303
            <span class="bp">self</span><span class="o">.</span><span class="n">handle_request</span><span class="p">()</span>
 
304
</pre></div>
 
305
</div>
 
306
</div>
 
307
</div>
 
308
 
 
309
 
 
310
          </div>
 
311
        </div>
 
312
      </div>
 
313
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
314
        <div class="sphinxsidebarwrapper">
 
315
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
316
  <ul>
 
317
<li><a class="reference internal" href="#">18.7. <code class="docutils literal"><span class="pre">asynchat</span></code> &#8212; Asynchronous socket command/response handler</a><ul>
 
318
<li><a class="reference internal" href="#asynchat-example">18.7.1. asynchat Example</a></li>
 
319
</ul>
 
320
</li>
 
321
</ul>
 
322
 
 
323
  <h4>Previous topic</h4>
 
324
  <p class="topless"><a href="asyncore.html"
 
325
                        title="previous chapter">18.6. <code class="docutils literal"><span class="pre">asyncore</span></code> &#8212; Asynchronous socket handler</a></p>
 
326
  <h4>Next topic</h4>
 
327
  <p class="topless"><a href="signal.html"
 
328
                        title="next chapter">18.8. <code class="docutils literal"><span class="pre">signal</span></code> &#8212; Set handlers for asynchronous events</a></p>
 
329
  <div role="note" aria-label="source link">
 
330
    <h3>This Page</h3>
 
331
    <ul class="this-page-menu">
 
332
      <li><a href="../bugs.html">Report a Bug</a></li>
 
333
      <li><a href="../_sources/library/asynchat.txt"
 
334
            rel="nofollow">Show Source</a></li>
 
335
    </ul>
 
336
  </div>
 
337
        </div>
 
338
      </div>
 
339
      <div class="clearer"></div>
 
340
    </div>  
 
341
    <div class="related" role="navigation" aria-label="related navigation">
 
342
      <h3>Navigation</h3>
 
343
      <ul>
 
344
        <li class="right" style="margin-right: 10px">
 
345
          <a href="../genindex.html" title="General Index"
 
346
             >index</a></li>
 
347
        <li class="right" >
 
348
          <a href="../py-modindex.html" title="Python Module Index"
 
349
             >modules</a> |</li>
 
350
        <li class="right" >
 
351
          <a href="signal.html" title="18.8. signal — Set handlers for asynchronous events"
 
352
             >next</a> |</li>
 
353
        <li class="right" >
 
354
          <a href="asyncore.html" title="18.6. asyncore — Asynchronous socket handler"
 
355
             >previous</a> |</li>
 
356
        <li><img src="../_static/py.png" alt=""
 
357
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
358
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
359
        <li>
 
360
          <span class="version_switcher_placeholder">3.5.2</span>
 
361
          <a href="../index.html">Documentation </a> &raquo;
 
362
        </li>
 
363
 
 
364
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
365
          <li class="nav-item nav-item-2"><a href="ipc.html" >18. Interprocess Communication and Networking</a> &raquo;</li>
 
366
    <li class="right">
 
367
        
 
368
 
 
369
    <div class="inline-search" style="display: none" role="search">
 
370
        <form class="inline-search" action="../search.html" method="get">
 
371
          <input placeholder="Quick search" type="text" name="q" />
 
372
          <input type="submit" value="Go" />
 
373
          <input type="hidden" name="check_keywords" value="yes" />
 
374
          <input type="hidden" name="area" value="default" />
 
375
        </form>
 
376
    </div>
 
377
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
378
         |
 
379
    </li>
 
380
 
 
381
      </ul>
 
382
    </div>  
 
383
    <div class="footer">
 
384
    &copy; <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
 
385
    <br />
 
386
    The Python Software Foundation is a non-profit corporation.
 
387
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
388
    <br />
 
389
    Last updated on Sep 23, 2016.
 
390
    <a href="../bugs.html">Found a bug</a>?
 
391
    <br />
 
392
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
393
    </div>
 
394
 
 
395
  </body>
 
396
</html>
 
 
b'\\ No newline at end of file'