Sophie

Sophie

distrib > Fedora > 13 > x86_64 > by-pkgid > 552d72b401c5b4a5a4c52922e7b31f2c > files > 89

python-eventlet-doc-0.9.12-1.fc13.noarch.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>wsgi – WSGI server &mdash; Eventlet v0.9.12 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:     '0.9.12',
        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="top" title="Eventlet v0.9.12 documentation" href="../index.html" />
    <link rel="up" title="Module Reference" href="../modules.html" />
    <link rel="next" title="Authors" href="../authors.html" />
    <link rel="prev" title="websocket – Websocket Server" href="websocket.html" /> 
  </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="../authors.html" title="Authors"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="websocket.html" title="websocket – Websocket Server"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">Eventlet v0.9.12 documentation</a> &raquo;</li>
          <li><a href="../modules.html" accesskey="U">Module Reference</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="wsgi-wsgi-server">
<h1><tt class="xref docutils literal"><span class="pre">wsgi</span></tt> &#8211; WSGI server<a class="headerlink" href="#wsgi-wsgi-server" title="Permalink to this headline">¶</a></h1>
<p>The wsgi module provides a simple an easy way to start an event-driven
<a class="reference external" href="http://wsgi.org/wsgi/">WSGI</a> server.  This can serve as an embedded
web server in an application, or as the basis for a more full-featured web
server package.  One such package is <a class="reference external" href="http://pypi.python.org/pypi/Spawning/">Spawning</a>.</p>
<p>To launch a wsgi server, simply create a socket and call <a title="eventlet.wsgi.server" class="reference internal" href="#eventlet.wsgi.server"><tt class="xref docutils literal"><span class="pre">eventlet.wsgi.server()</span></tt></a> with it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">eventlet</span> <span class="kn">import</span> <span class="n">wsgi</span>
<span class="kn">import</span> <span class="nn">eventlet</span>

<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">start_response</span><span class="p">(</span><span class="s">&#39;200 OK&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="s">&#39;text/plain&#39;</span><span class="p">)])</span>
    <span class="k">return</span> <span class="p">[</span><span class="s">&#39;Hello, World!</span><span class="se">\r\n</span><span class="s">&#39;</span><span class="p">]</span>

<span class="n">wsgi</span><span class="o">.</span><span class="n">server</span><span class="p">(</span><span class="n">eventlet</span><span class="o">.</span><span class="n">listen</span><span class="p">((</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="mi">8090</span><span class="p">)),</span> <span class="n">hello_world</span><span class="p">)</span>
</pre></div>
</div>
<p>You can find a slightly more elaborate version of this code in the file
<tt class="docutils literal"><span class="pre">examples/wsgi.py</span></tt>.</p>
<div class="section" id="non-standard-extension-to-support-post-hooks">
<h2>Non-Standard Extension to Support Post Hooks<a class="headerlink" href="#non-standard-extension-to-support-post-hooks" title="Permalink to this headline">¶</a></h2>
<p>Eventlet&#8217;s WSGI server supports a non-standard extension to the WSGI
specification where <tt class="docutils literal"><span class="pre">env['eventlet.posthooks']</span></tt> contains an array of
<cite>post hooks</cite> that will be called after fully sending a response. Each post hook
is a tuple of <tt class="docutils literal"><span class="pre">(func,</span> <span class="pre">args,</span> <span class="pre">kwargs)</span></tt> and the <cite>func</cite> will be called with
the WSGI environment dictionary, followed by the <cite>args</cite> and then the <cite>kwargs</cite>
in the post hook.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">eventlet</span> <span class="kn">import</span> <span class="n">wsgi</span>
<span class="kn">import</span> <span class="nn">eventlet</span>

<span class="k">def</span> <span class="nf">hook</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">kwarg3</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">kwarg4</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;Hook called: </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">kwarg3</span><span class="p">,</span> <span class="n">kwarg4</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">env</span><span class="p">[</span><span class="s">&#39;eventlet.posthooks&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
        <span class="p">(</span><span class="n">hook</span><span class="p">,</span> <span class="p">(</span><span class="s">&#39;arg1&#39;</span><span class="p">,</span> <span class="s">&#39;arg2&#39;</span><span class="p">),</span> <span class="p">{</span><span class="s">&#39;kwarg3&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s">&#39;kwarg4&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">}))</span>
    <span class="n">start_response</span><span class="p">(</span><span class="s">&#39;200 OK&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="s">&#39;text/plain&#39;</span><span class="p">)])</span>
    <span class="k">return</span> <span class="p">[</span><span class="s">&#39;Hello, World!</span><span class="se">\r\n</span><span class="s">&#39;</span><span class="p">]</span>

<span class="n">wsgi</span><span class="o">.</span><span class="n">server</span><span class="p">(</span><span class="n">eventlet</span><span class="o">.</span><span class="n">listen</span><span class="p">((</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="mi">8090</span><span class="p">)),</span> <span class="n">hello_world</span><span class="p">)</span>
</pre></div>
</div>
<p>The above code will print the WSGI environment and the other passed function
arguments for every request processed.</p>
<p>Post hooks are useful when code needs to be executed after a response has been
fully sent to the client (or when the client disconnects early). One example is
for more accurate logging of bandwidth used, as client disconnects use less
bandwidth than the actual Content-Length.</p>
</div>
<div class="section" id="module-eventlet.wsgi">
<h2>API<a class="headerlink" href="#module-eventlet.wsgi" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="eventlet.wsgi.server">
<tt class="descclassname">eventlet.wsgi.</tt><tt class="descname">server</tt><big>(</big><em>sock</em>, <em>site</em>, <em>log=None</em>, <em>environ=None</em>, <em>max_size=None</em>, <em>max_http_version='HTTP/1.1'</em>, <em>protocol=&lt;class eventlet.wsgi.HttpProtocol at 0x9deef2c&gt;</em>, <em>server_event=None</em>, <em>minimum_chunk_size=None</em>, <em>log_x_forwarded_for=True</em>, <em>custom_pool=None</em>, <em>keepalive=True</em>, <em>log_format='%(client_ip)s - -</em><span class="optional">[</span>, <em>%(date_time)s</em><span class="optional">]</span>, <em>&quot;%(request_line)s&quot; %(status_code)s %(body_length)s %(wall_seconds).6f'</em><big>)</big><a class="headerlink" href="#eventlet.wsgi.server" title="Permalink to this definition">¶</a></dt>
<dd><p>Start up a wsgi server handling requests from the supplied server
socket.  This function loops forever.  The <em>sock</em> object will be closed after server exits,
but the underlying file descriptor will remain open, so if you have a dup() of <em>sock</em>,
it will remain usable.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><em>sock</em> &#8211; Server socket, must be already bound to a port and listening.</li>
<li><em>site</em> &#8211; WSGI application function.</li>
<li><em>log</em> &#8211; File-like object that logs should be written to.  If not specified, sys.stderr is used.</li>
<li><em>environ</em> &#8211; Additional parameters that go into the environ dictionary of every request.</li>
<li><em>max_size</em> &#8211; Maximum number of client connections opened at any time by this server.</li>
<li><em>max_http_version</em> &#8211; Set to &#8220;HTTP/1.0&#8221; to make the server pretend it only supports HTTP 1.0.  This can help with applications or clients that don&#8217;t behave properly using HTTP 1.1.</li>
<li><em>protocol</em> &#8211; Protocol class.  Deprecated.</li>
<li><em>server_event</em> &#8211; Used to collect the Server object.  Deprecated.</li>
<li><em>minimum_chunk_size</em> &#8211; Minimum size in bytes for http chunks.  This  can be used to improve performance of applications which yield many small strings, though using it technically violates the WSGI spec.</li>
<li><em>log_x_forwarded_for</em> &#8211; If True (the default), logs the contents of the x-forwarded-for header in addition to the actual client ip address in the &#8216;client_ip&#8217; field of the log line.</li>
<li><em>custom_pool</em> &#8211; A custom GreenPool instance which is used to spawn client green threads.  If this is supplied, max_size is ignored.</li>
<li><em>keepalive</em> &#8211; If set to False, disables keepalives on the server; all connections will be closed after serving one request.</li>
<li><em>log_format</em> &#8211; A python format string that is used as the template to generate log lines.  The following values can be formatted into it: client_ip, date_time, request_line, status_code, body_length, wall_seconds.  Look the default for an example of how to use this.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="eventlet.wsgi.format_date_time">
<tt class="descclassname">eventlet.wsgi.</tt><tt class="descname">format_date_time</tt><big>(</big><em>timestamp</em><big>)</big><a class="headerlink" href="#eventlet.wsgi.format_date_time" title="Permalink to this definition">¶</a></dt>
<dd>Formats a unix timestamp into an HTTP standard string.</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#"><tt class="docutils literal"><span class="pre">wsgi</span></tt> &#8211; WSGI server</a><ul>
<li><a class="reference external" href="#non-standard-extension-to-support-post-hooks">Non-Standard Extension to Support Post Hooks</a></li>
<li><a class="reference external" href="#module-eventlet.wsgi">API</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="websocket.html"
                                  title="previous chapter"><tt class="docutils literal docutils literal docutils literal"><span class="pre">websocket</span></tt> &#8211; Websocket Server</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="../authors.html"
                                  title="next chapter">Authors</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/modules/wsgi.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="../authors.html" title="Authors"
             >next</a> |</li>
        <li class="right" >
          <a href="websocket.html" title="websocket – Websocket Server"
             >previous</a> |</li>
        <li><a href="../index.html">Eventlet v0.9.12 documentation</a> &raquo;</li>
          <li><a href="../modules.html" >Module Reference</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2005-2010, Eventlet Contributors.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
    </div>
  </body>
</html>