Sophie

Sophie

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

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>websocket – Websocket 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="wsgi – WSGI server" href="wsgi.html" />
    <link rel="prev" title="timeout – Universal Timeouts" href="timeout.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="wsgi.html" title="wsgi – WSGI server"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="timeout.html" title="timeout – Universal Timeouts"
             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="module-eventlet.websocket">
<h1><tt class="xref docutils literal"><span class="pre">websocket</span></tt> &#8211; Websocket Server<a class="headerlink" href="#module-eventlet.websocket" title="Permalink to this headline">¶</a></h1>
<p>This module provides a simple way to create a <cite>websocket
&lt;http://dev.w3.org/html5/websockets/&gt;</cite> server.  It works with a few
tweaks in the <a title="" class="reference external" href="wsgi.html#module-eventlet.wsgi"><tt class="xref docutils literal"><span class="pre">wsgi</span></tt></a> module that allow websockets to
coexist with other WSGI applications.</p>
<p>To create a websocket server, simply decorate a handler method with
<tt class="xref docutils literal"><span class="pre">WebSocketWSGI</span></tt> and use it as a wsgi application:</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="p">,</span> <span class="n">websocket</span>
<span class="kn">import</span> <span class="nn">eventlet</span>

<span class="nd">@websocket.WebSocketWSGI</span>
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">ws</span><span class="p">):</span>
    <span class="n">ws</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s">&quot;hello world&quot;</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/websocket.py</span></tt>.</p>
<p><strong>Note</strong> that the web socket spec is still under development, and it
will be necessary to change the way that this module works in response
to spec changes.</p>
<dl class="class">
<dt id="eventlet.websocket.WebSocketWSGI">
<em class="property">class </em><tt class="descclassname">eventlet.websocket.</tt><tt class="descname">WebSocketWSGI</tt><big>(</big><em>handler</em><big>)</big><a class="headerlink" href="#eventlet.websocket.WebSocketWSGI" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps a websocket handler function in a WSGI application.</p>
<p>Use it like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@websocket.WebSocketWSGI</span>
<span class="k">def</span> <span class="nf">my_handler</span><span class="p">(</span><span class="n">ws</span><span class="p">):</span>
    <span class="n">from_browser</span> <span class="o">=</span> <span class="n">ws</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
    <span class="n">ws</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s">&quot;from server&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The single argument to the function will be an instance of
<a title="eventlet.websocket.WebSocket" class="reference internal" href="#eventlet.websocket.WebSocket"><tt class="xref docutils literal"><span class="pre">WebSocket</span></tt></a>.  To close the socket, simply return from the
function.  Note that the server will log the websocket request at
the time of closure.</p>
</dd></dl>

<dl class="class">
<dt id="eventlet.websocket.WebSocket">
<em class="property">class </em><tt class="descclassname">eventlet.websocket.</tt><tt class="descname">WebSocket</tt><big>(</big><em>sock</em>, <em>environ</em>, <em>version=76</em><big>)</big><a class="headerlink" href="#eventlet.websocket.WebSocket" title="Permalink to this definition">¶</a></dt>
<dd><p>A websocket object that handles the details of
serialization/deserialization to the socket.</p>
<p>The primary way to interact with a <a title="eventlet.websocket.WebSocket" class="reference internal" href="#eventlet.websocket.WebSocket"><tt class="xref docutils literal"><span class="pre">WebSocket</span></tt></a> object is to
call <a title="eventlet.websocket.WebSocket.send" class="reference internal" href="#eventlet.websocket.WebSocket.send"><tt class="xref docutils literal"><span class="pre">send()</span></tt></a> and <a title="eventlet.websocket.WebSocket.wait" class="reference internal" href="#eventlet.websocket.WebSocket.wait"><tt class="xref docutils literal"><span class="pre">wait()</span></tt></a> in order to pass messages back
and forth with the browser.  Also available are the following
properties:</p>
<dl class="docutils">
<dt>path</dt>
<dd>The path value of the request.  This is the same as the WSGI PATH_INFO variable, but more convenient.</dd>
<dt>protocol</dt>
<dd>The value of the Websocket-Protocol header.</dd>
<dt>origin</dt>
<dd>The value of the &#8216;Origin&#8217; header.</dd>
<dt>environ</dt>
<dd>The full WSGI environment for this request.</dd>
</dl>
<dl class="method">
<dt id="eventlet.websocket.WebSocket.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#eventlet.websocket.WebSocket.close" title="Permalink to this definition">¶</a></dt>
<dd>Forcibly close the websocket; generally it is preferable to
return from the handler method.</dd></dl>

<dl class="method">
<dt id="eventlet.websocket.WebSocket.send">
<tt class="descname">send</tt><big>(</big><em>message</em><big>)</big><a class="headerlink" href="#eventlet.websocket.WebSocket.send" title="Permalink to this definition">¶</a></dt>
<dd>Send a message to the browser.  <em>message</em> should be
convertable to a string; unicode objects should be encodable
as utf-8.</dd></dl>

<dl class="method">
<dt id="eventlet.websocket.WebSocket.wait">
<tt class="descname">wait</tt><big>(</big><big>)</big><a class="headerlink" href="#eventlet.websocket.WebSocket.wait" title="Permalink to this definition">¶</a></dt>
<dd>Waits for and deserializes messages. Returns a single
message; the oldest not yet processed.</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="timeout.html"
                                  title="previous chapter"><tt class="docutils literal docutils literal docutils literal"><span class="pre">timeout</span></tt> &#8211; Universal Timeouts</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="wsgi.html"
                                  title="next chapter"><tt class="docutils literal docutils literal"><span class="pre">wsgi</span></tt> &#8211; WSGI server</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/modules/websocket.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="wsgi.html" title="wsgi – WSGI server"
             >next</a> |</li>
        <li class="right" >
          <a href="timeout.html" title="timeout – Universal Timeouts"
             >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>