Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > e870e6598e1c7e3918555a3d0ba5f3d4 > files > 568

python3-docs-3.1.1-2mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>17.6. asynchat — Asynchronous socket command/response handler &mdash; Python v3.1.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.1.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.1.1 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.1.1 documentation" href="../index.html" />
    <link rel="up" title="17. Interprocess Communication and Networking" href="ipc.html" />
    <link rel="next" title="18. Internet Data Handling" href="netdata.html" />
    <link rel="prev" title="17.5. asyncore — Asynchronous socket handler" href="asyncore.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="netdata.html" title="18. Internet Data Handling"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="asyncore.html" title="17.5. asyncore — Asynchronous socket handler"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="ipc.html" accesskey="U">17. Interprocess Communication and Networking</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-asynchat">
<h1>17.6. <tt class="xref docutils literal"><span class="pre">asynchat</span></tt> &#8212; Asynchronous socket command/response handler<a class="headerlink" href="#module-asynchat" title="Permalink to this headline">¶</a></h1>
<p>This module builds on the <a title="A base class for developing asynchronous socket handling services." class="reference external" href="asyncore.html#module-asyncore"><tt class="xref docutils literal"><span class="pre">asyncore</span></tt></a> infrastructure, simplifying
asynchronous clients and servers and making it easier to handle protocols
whose elements are terminated by arbitrary strings, or are of variable length.
<tt class="xref docutils literal"><span class="pre">asynchat</span></tt> defines the abstract class <a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a> that you
subclass, providing implementations of the <tt class="xref docutils literal"><span class="pre">collect_incoming_data()</span></tt> and
<tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt> methods. It uses the same asynchronous loop as
<a title="A base class for developing asynchronous socket handling services." class="reference external" href="asyncore.html#module-asyncore"><tt class="xref docutils literal"><span class="pre">asyncore</span></tt></a>, and the two types of channel, <a title="asyncore.dispatcher" class="reference external" href="asyncore.html#asyncore.dispatcher"><tt class="xref docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a>
and <a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">asynchat.async_chat</span></tt></a>, can freely be mixed in the channel map.
Typically an <a title="asyncore.dispatcher" class="reference external" href="asyncore.html#asyncore.dispatcher"><tt class="xref docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a> server channel generates new
<a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">asynchat.async_chat</span></tt></a> channel objects as it receives incoming
connection requests.</p>
<dl class="class">
<dt id="asynchat.async_chat">
<em class="property">
class </em><tt class="descclassname">asynchat.</tt><tt class="descname">async_chat</tt><a class="headerlink" href="#asynchat.async_chat" title="Permalink to this definition">¶</a></dt>
<dd><p>This class is an abstract subclass of <a title="asyncore.dispatcher" class="reference external" href="asyncore.html#asyncore.dispatcher"><tt class="xref docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a>. To make
practical use of the code you must subclass <a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a>, providing
meaningful <a title="asynchat.async_chat.collect_incoming_data" class="reference internal" href="#asynchat.async_chat.collect_incoming_data"><tt class="xref docutils literal"><span class="pre">collect_incoming_data()</span></tt></a> and <a title="asynchat.async_chat.found_terminator" class="reference internal" href="#asynchat.async_chat.found_terminator"><tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt></a>
methods.
The <a title="asyncore.dispatcher" class="reference external" href="asyncore.html#asyncore.dispatcher"><tt class="xref docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a> methods can be used, although not all make
sense in a message/response context.</p>
<p>Like <a title="asyncore.dispatcher" class="reference external" href="asyncore.html#asyncore.dispatcher"><tt class="xref docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a>, <a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a> defines a set of
events that are generated by an analysis of socket conditions after a
<tt class="xref docutils literal"><span class="pre">select()</span></tt> call. Once the polling loop has been started the
<a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a> object&#8217;s methods are called by the event-processing
framework with no action on the part of the programmer.</p>
<p>Two class attributes can be modified, to improve performance, or possibly
even to conserve memory.</p>
<dl class="data">
<dt id="asynchat.async_chat.ac_in_buffer_size">
<tt class="descname">ac_in_buffer_size</tt><a class="headerlink" href="#asynchat.async_chat.ac_in_buffer_size" title="Permalink to this definition">¶</a></dt>
<dd>The asynchronous input buffer size (default <tt class="docutils literal"><span class="pre">4096</span></tt>).</dd></dl>

<dl class="data">
<dt id="asynchat.async_chat.ac_out_buffer_size">
<tt class="descname">ac_out_buffer_size</tt><a class="headerlink" href="#asynchat.async_chat.ac_out_buffer_size" title="Permalink to this definition">¶</a></dt>
<dd>The asynchronous output buffer size (default <tt class="docutils literal"><span class="pre">4096</span></tt>).</dd></dl>

<p>Unlike <a title="asyncore.dispatcher" class="reference external" href="asyncore.html#asyncore.dispatcher"><tt class="xref docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a>, <a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a> allows you to
define a first-in-first-out queue (fifo) of <em>producers</em>. A producer need
have only one method, <tt class="xref docutils literal"><span class="pre">more()</span></tt>, which should return data to be
transmitted on the channel.
The producer indicates exhaustion (<em>i.e.</em> that it contains no more data) by
having its <tt class="xref docutils literal"><span class="pre">more()</span></tt> method return the empty string. At this point the
<a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a> object removes the producer from the fifo and starts
using the next producer, if any. When the producer fifo is empty the
<a title="asynchat.async_chat.handle_write" class="reference internal" href="#asynchat.async_chat.handle_write"><tt class="xref docutils literal"><span class="pre">handle_write()</span></tt></a> method does nothing. You use the channel object&#8217;s
<a title="asynchat.async_chat.set_terminator" class="reference internal" href="#asynchat.async_chat.set_terminator"><tt class="xref docutils literal"><span class="pre">set_terminator()</span></tt></a> method to describe how to recognize the end of, or
an important breakpoint in, an incoming transmission from the remote
endpoint.</p>
<p>To build a functioning <a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a> subclass your  input methods
<a title="asynchat.async_chat.collect_incoming_data" class="reference internal" href="#asynchat.async_chat.collect_incoming_data"><tt class="xref docutils literal"><span class="pre">collect_incoming_data()</span></tt></a> and <a title="asynchat.async_chat.found_terminator" class="reference internal" href="#asynchat.async_chat.found_terminator"><tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt></a> must handle the
data that the channel receives asynchronously. The methods are described
below.</p>
</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.close_when_done">
<tt class="descclassname">async_chat.</tt><tt class="descname">close_when_done</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.close_when_done" title="Permalink to this definition">¶</a></dt>
<dd>Pushes a <tt class="xref docutils literal"><span class="pre">None</span></tt> on to the producer fifo. When this producer is popped off
the fifo it causes the channel to be closed.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.collect_incoming_data">
<tt class="descclassname">async_chat.</tt><tt class="descname">collect_incoming_data</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#asynchat.async_chat.collect_incoming_data" title="Permalink to this definition">¶</a></dt>
<dd>Called with <em>data</em> holding an arbitrary amount of received data.  The
default method, which must be overridden, raises a
<a title="exceptions.NotImplementedError" class="reference external" href="exceptions.html#exceptions.NotImplementedError"><tt class="xref docutils literal"><span class="pre">NotImplementedError</span></tt></a> exception.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat._collect_incoming_data">
<tt class="descclassname">async_chat.</tt><tt class="descname">_collect_incoming_data</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#asynchat.async_chat._collect_incoming_data" title="Permalink to this definition">¶</a></dt>
<dd>Sample implementation of a data collection rutine to be used in conjunction
with <a title="asynchat.async_chat._get_data" class="reference internal" href="#asynchat.async_chat._get_data"><tt class="xref docutils literal"><span class="pre">_get_data()</span></tt></a> in a user-specified <a title="asynchat.async_chat.found_terminator" class="reference internal" href="#asynchat.async_chat.found_terminator"><tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.discard_buffers">
<tt class="descclassname">async_chat.</tt><tt class="descname">discard_buffers</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.discard_buffers" title="Permalink to this definition">¶</a></dt>
<dd>In emergencies this method will discard any data held in the input and/or
output buffers and the producer fifo.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.found_terminator">
<tt class="descclassname">async_chat.</tt><tt class="descname">found_terminator</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.found_terminator" title="Permalink to this definition">¶</a></dt>
<dd>Called when the incoming data stream  matches the termination condition set
by <a title="asynchat.async_chat.set_terminator" class="reference internal" href="#asynchat.async_chat.set_terminator"><tt class="xref docutils literal"><span class="pre">set_terminator()</span></tt></a>. The default method, which must be overridden,
raises a <a title="exceptions.NotImplementedError" class="reference external" href="exceptions.html#exceptions.NotImplementedError"><tt class="xref docutils literal"><span class="pre">NotImplementedError</span></tt></a> exception. The buffered input data
should be available via an instance attribute.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat._get_data">
<tt class="descclassname">async_chat.</tt><tt class="descname">_get_data</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat._get_data" title="Permalink to this definition">¶</a></dt>
<dd>Will return and clear the data received with the sample
<a title="asynchat.async_chat._collect_incoming_data" class="reference internal" href="#asynchat.async_chat._collect_incoming_data"><tt class="xref docutils literal"><span class="pre">_collect_incoming_data()</span></tt></a> implementation.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.get_terminator">
<tt class="descclassname">async_chat.</tt><tt class="descname">get_terminator</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.get_terminator" title="Permalink to this definition">¶</a></dt>
<dd>Returns the current terminator for the channel.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.handle_close">
<tt class="descclassname">async_chat.</tt><tt class="descname">handle_close</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.handle_close" title="Permalink to this definition">¶</a></dt>
<dd>Called when the channel is closed. The default method silently closes the
channel&#8217;s socket.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.handle_read">
<tt class="descclassname">async_chat.</tt><tt class="descname">handle_read</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.handle_read" title="Permalink to this definition">¶</a></dt>
<dd>Called when a read event fires on the channel&#8217;s socket in the asynchronous
loop.  The default method checks for the termination condition established
by <a title="asynchat.async_chat.set_terminator" class="reference internal" href="#asynchat.async_chat.set_terminator"><tt class="xref docutils literal"><span class="pre">set_terminator()</span></tt></a>, which can be either the appearance of a
particular string in the input stream or the receipt of a particular number
of characters.  When the terminator is found, <a title="asynchat.async_chat.handle_read" class="reference internal" href="#asynchat.async_chat.handle_read"><tt class="xref docutils literal"><span class="pre">handle_read()</span></tt></a> calls the
<a title="asynchat.async_chat.found_terminator" class="reference internal" href="#asynchat.async_chat.found_terminator"><tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt></a> method after calling <a title="asynchat.async_chat.collect_incoming_data" class="reference internal" href="#asynchat.async_chat.collect_incoming_data"><tt class="xref docutils literal"><span class="pre">collect_incoming_data()</span></tt></a>
with any data preceding the terminating condition.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.handle_write">
<tt class="descclassname">async_chat.</tt><tt class="descname">handle_write</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.handle_write" title="Permalink to this definition">¶</a></dt>
<dd>Called when the application may write data to the channel.   The default
method calls the <tt class="xref docutils literal"><span class="pre">initiate_send()</span></tt> method, which in turn will call
<a title="asynchat.async_chat.refill_buffer" class="reference internal" href="#asynchat.async_chat.refill_buffer"><tt class="xref docutils literal"><span class="pre">refill_buffer()</span></tt></a> to collect data from the producer fifo associated
with the channel.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.push">
<tt class="descclassname">async_chat.</tt><tt class="descname">push</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#asynchat.async_chat.push" title="Permalink to this definition">¶</a></dt>
<dd>Creates a <a title="asynchat.simple_producer" class="reference internal" href="#asynchat.simple_producer"><tt class="xref docutils literal"><span class="pre">simple_producer</span></tt></a> object (<em>see below</em>) containing the data
and pushes it on to the channel&#8217;s <tt class="docutils literal"><span class="pre">producer_fifo</span></tt> to ensure its
transmission.  This is all you need to do to have the channel write the
data out to the network, although it is possible to use your own producers
in more complex schemes to implement encryption and chunking, for example.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.push_with_producer">
<tt class="descclassname">async_chat.</tt><tt class="descname">push_with_producer</tt><big>(</big><em>producer</em><big>)</big><a class="headerlink" href="#asynchat.async_chat.push_with_producer" title="Permalink to this definition">¶</a></dt>
<dd>Takes a producer object and adds it to the producer fifo associated with
the channel.  When all currently-pushed producers have been exhausted the
channel will consume this producer&#8217;s data by calling its <tt class="xref docutils literal"><span class="pre">more()</span></tt>
method and send the data to the remote endpoint.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.readable">
<tt class="descclassname">async_chat.</tt><tt class="descname">readable</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.readable" title="Permalink to this definition">¶</a></dt>
<dd>Should return <tt class="xref docutils literal"><span class="pre">True</span></tt> for the channel to be included in the set of
channels tested by the <tt class="xref docutils literal"><span class="pre">select()</span></tt> loop for readability.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.refill_buffer">
<tt class="descclassname">async_chat.</tt><tt class="descname">refill_buffer</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.refill_buffer" title="Permalink to this definition">¶</a></dt>
<dd>Refills the output buffer by calling the <tt class="xref docutils literal"><span class="pre">more()</span></tt> method of the
producer at the head of the fifo.  If it is exhausted then the producer is
popped off the fifo and the next producer is activated.  If the current
producer is, or becomes, <tt class="xref docutils literal"><span class="pre">None</span></tt> then the channel is closed.</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.set_terminator">
<tt class="descclassname">async_chat.</tt><tt class="descname">set_terminator</tt><big>(</big><em>term</em><big>)</big><a class="headerlink" href="#asynchat.async_chat.set_terminator" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the terminating condition to be recognized on the channel.  <tt class="docutils literal"><span class="pre">term</span></tt>
may be any of three types of value, corresponding to three different ways
to handle incoming protocol data.</p>
<table border="1" class="docutils">
<colgroup>
<col width="20%" />
<col width="80%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">term</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><em>string</em></td>
<td>Will call <a title="asynchat.async_chat.found_terminator" class="reference internal" href="#asynchat.async_chat.found_terminator"><tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt></a> when the
string is found in the input stream</td>
</tr>
<tr><td><em>integer</em></td>
<td>Will call <a title="asynchat.async_chat.found_terminator" class="reference internal" href="#asynchat.async_chat.found_terminator"><tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt></a> when the
indicated number of characters have been
received</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">None</span></tt></td>
<td>The channel continues to collect data
forever</td>
</tr>
</tbody>
</table>
<p>Note that any data following the terminator will be available for reading
by the channel after <a title="asynchat.async_chat.found_terminator" class="reference internal" href="#asynchat.async_chat.found_terminator"><tt class="xref docutils literal"><span class="pre">found_terminator()</span></tt></a> is called.</p>
</dd></dl>

<dl class="method">
<dt id="asynchat.async_chat.writable">
<tt class="descclassname">async_chat.</tt><tt class="descname">writable</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.async_chat.writable" title="Permalink to this definition">¶</a></dt>
<dd>Should return <tt class="xref docutils literal"><span class="pre">True</span></tt> as long as items remain on the producer fifo, or the
channel is connected and the channel&#8217;s output buffer is non-empty.</dd></dl>

<div class="section" id="asynchat-auxiliary-classes-and-functions">
<h2>17.6.1. asynchat - Auxiliary Classes and Functions<a class="headerlink" href="#asynchat-auxiliary-classes-and-functions" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="asynchat.simple_producer">
<em class="property">
class </em><tt class="descclassname">asynchat.</tt><tt class="descname">simple_producer</tt><big>(</big><em>data</em>, <em>buffer_size=512</em><big>)</big><a class="headerlink" href="#asynchat.simple_producer" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a title="asynchat.simple_producer" class="reference internal" href="#asynchat.simple_producer"><tt class="xref docutils literal"><span class="pre">simple_producer</span></tt></a> takes a chunk of data and an optional buffer
size.  Repeated calls to its <a title="asynchat.simple_producer.more" class="reference internal" href="#asynchat.simple_producer.more"><tt class="xref docutils literal"><span class="pre">more()</span></tt></a> method yield successive chunks of
the data no larger than <em>buffer_size</em>.</p>
<dl class="method">
<dt id="asynchat.simple_producer.more">
<tt class="descname">more</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.simple_producer.more" title="Permalink to this definition">¶</a></dt>
<dd>Produces the next chunk of information from the producer, or returns the
empty string.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="asynchat.fifo">
<em class="property">
class </em><tt class="descclassname">asynchat.</tt><tt class="descname">fifo</tt><big>(</big><em>list=None</em><big>)</big><a class="headerlink" href="#asynchat.fifo" title="Permalink to this definition">¶</a></dt>
<dd><p>Each channel maintains a <a title="asynchat.fifo" class="reference internal" href="#asynchat.fifo"><tt class="xref docutils literal"><span class="pre">fifo</span></tt></a> holding data which has been pushed
by the application but not yet popped for writing to the channel.  A
<a title="asynchat.fifo" class="reference internal" href="#asynchat.fifo"><tt class="xref docutils literal"><span class="pre">fifo</span></tt></a> is a list used to hold data and/or producers until they are
required.  If the <em>list</em> argument is provided then it should contain
producers or data items to be written to the channel.</p>
<dl class="method">
<dt id="asynchat.fifo.is_empty">
<tt class="descname">is_empty</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.fifo.is_empty" title="Permalink to this definition">¶</a></dt>
<dd>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if and only if the fifo is empty.</dd></dl>

<dl class="method">
<dt id="asynchat.fifo.first">
<tt class="descname">first</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.fifo.first" title="Permalink to this definition">¶</a></dt>
<dd>Returns the least-recently <a title="asynchat.fifo.push" class="reference internal" href="#asynchat.fifo.push"><tt class="xref docutils literal"><span class="pre">push()</span></tt></a>ed item from the fifo.</dd></dl>

<dl class="method">
<dt id="asynchat.fifo.push">
<tt class="descname">push</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#asynchat.fifo.push" title="Permalink to this definition">¶</a></dt>
<dd>Adds the given data (which may be a string or a producer object) to the
producer fifo.</dd></dl>

<dl class="method">
<dt id="asynchat.fifo.pop">
<tt class="descname">pop</tt><big>(</big><big>)</big><a class="headerlink" href="#asynchat.fifo.pop" title="Permalink to this definition">¶</a></dt>
<dd>If the fifo is not empty, returns <tt class="docutils literal"><span class="pre">True,</span> <span class="pre">first()</span></tt>, deleting the popped
item.  Returns <tt class="docutils literal"><span class="pre">False,</span> <span class="pre">None</span></tt> for an empty fifo.</dd></dl>

</dd></dl>

<p>The <tt class="xref docutils literal"><span class="pre">asynchat</span></tt> module also defines one utility function, which may be of
use in network and textual analysis operations.</p>
<dl class="function">
<dt id="asynchat.find_prefix_at_end">
<tt class="descclassname">asynchat.</tt><tt class="descname">find_prefix_at_end</tt><big>(</big><em>haystack</em>, <em>needle</em><big>)</big><a class="headerlink" href="#asynchat.find_prefix_at_end" title="Permalink to this definition">¶</a></dt>
<dd>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if string <em>haystack</em> ends with any non-empty prefix of
string <em>needle</em>.</dd></dl>

</div>
<div class="section" id="asynchat-example">
<span id="id1"></span><h2>17.6.2. asynchat Example<a class="headerlink" href="#asynchat-example" title="Permalink to this headline">¶</a></h2>
<p>The following partial example shows how HTTP requests can be read with
<a title="asynchat.async_chat" class="reference internal" href="#asynchat.async_chat"><tt class="xref docutils literal"><span class="pre">async_chat</span></tt></a>.  A web server might create an
<tt class="xref docutils literal"><span class="pre">http_request_handler</span></tt> object for each incoming client connection.
Notice that initially the channel terminator is set to match the blank line at
the end of the HTTP headers, and a flag indicates that the headers are being
read.</p>
<p>Once the headers have been read, if the request is of type POST (indicating
that further data are present in the input stream) then the
<tt class="docutils literal"><span class="pre">Content-Length:</span></tt> header is used to set a numeric terminator to read the
right amount of data from the channel.</p>
<p>The <tt class="xref docutils literal"><span class="pre">handle_request()</span></tt> method is called once all relevant input has been
marshalled, after setting the channel terminator to <tt class="xref docutils literal"><span class="pre">None</span></tt> to ensure that
any extraneous data sent by the web client are ignored.</p>
<div class="highlight-python3"><pre>class http_request_handler(asynchat.async_chat):

    def __init__(self, sock, addr, sessions, log):
        asynchat.async_chat.__init__(self, sock=sock)
        self.addr = addr
        self.sessions = sessions
        self.ibuffer = []
        self.obuffer = b""
        self.set_terminator(b"\r\n\r\n")
        self.reading_headers = True
        self.handling = False
        self.cgi_data = None
        self.log = log

    def collect_incoming_data(self, data):
        """Buffer the data"""
        self.ibuffer.append(data)

    def found_terminator(self):
        if self.reading_headers:
            self.reading_headers = False
            self.parse_headers("".join(self.ibuffer))
            self.ibuffer = []
            if self.op.upper() == b"POST":
                clen = self.headers.getheader("content-length")
                self.set_terminator(int(clen))
            else:
                self.handling = True
                self.set_terminator(None)
                self.handle_request()
        elif not self.handling:
            self.set_terminator(None) # browsers sometimes over-send
            self.cgi_data = parse(self.headers, b"".join(self.ibuffer))
            self.handling = True
            self.ibuffer = []
            self.handle_request()</pre>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">17.6. <tt class="docutils literal"><span class="pre">asynchat</span></tt> &#8212; Asynchronous socket command/response handler</a><ul>
<li><a class="reference external" href="#asynchat-auxiliary-classes-and-functions">17.6.1. asynchat - Auxiliary Classes and Functions</a></li>
<li><a class="reference external" href="#asynchat-example">17.6.2. asynchat Example</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="asyncore.html"
                                  title="previous chapter">17.5. <tt class="docutils literal"><span class="pre">asyncore</span></tt> &#8212; Asynchronous socket handler</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="netdata.html"
                                  title="next chapter">18. Internet Data Handling</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/asynchat.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="netdata.html" title="18. Internet Data Handling"
             >next</a> |</li>
        <li class="right" >
          <a href="asyncore.html" title="17.5. asyncore — Asynchronous socket handler"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="ipc.html" >17. Interprocess Communication and Networking</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 16, 2009.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
    </div>

  </body>
</html>