<!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 — Eventlet 0.13.0 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.13.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="top" title="Eventlet 0.13.0 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="../py-modindex.html" title="Python Module Index" >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 0.13.0 documentation</a> »</li> <li><a href="../modules.html" accesskey="U">Module Reference</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="websocket-websocket-server"> <h1><tt class="xref py py-mod docutils literal"><span class="pre">websocket</span></tt> – Websocket Server<a class="headerlink" href="#websocket-websocket-server" title="Permalink to this headline">¶</a></h1> <p>This module provides a simple way to create a <a class="reference external" href="http://dev.w3.org/html5/websockets/">websocket</a> server. It works with a few tweaks in the <a class="reference internal" href="wsgi.html#module-eventlet.wsgi" title="eventlet.wsgi"><tt class="xref py py-mod 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 py py-class 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">"hello world"</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">''</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>As of version 0.9.13, eventlet.websocket supports SSL websockets; all that’s necessary is to use an <a class="reference internal" href="wsgi.html#wsgi-ssl"><em>SSL wsgi server</em></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">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> </div> <span class="target" id="module-eventlet.websocket"></span><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">"from server"</span><span class="p">)</span> </pre></div> </div> <p>The single argument to the function will be an instance of <a class="reference internal" href="#eventlet.websocket.WebSocket" title="eventlet.websocket.WebSocket"><tt class="xref py py-class 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 class="reference internal" href="#eventlet.websocket.WebSocket" title="eventlet.websocket.WebSocket"><tt class="xref py py-class docutils literal"><span class="pre">WebSocket</span></tt></a> object is to call <a class="reference internal" href="#eventlet.websocket.WebSocket.send" title="eventlet.websocket.WebSocket.send"><tt class="xref py py-meth docutils literal"><span class="pre">send()</span></tt></a> and <a class="reference internal" href="#eventlet.websocket.WebSocket.wait" title="eventlet.websocket.WebSocket.wait"><tt class="xref py py-meth 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 ‘Origin’ 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><p>Forcibly close the websocket; generally it is preferable to return from the handler method.</p> </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><p>Send a message to the browser.</p> <p><em>message</em> should be convertable to a string; unicode objects should be encodable as utf-8. Raises socket.error with errno of 32 (broken pipe) if the socket has already been closed by the client.</p> </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><p>Waits for and deserializes messages.</p> <p>Returns a single message; the oldest not yet processed. If the client has already closed the connection, returns None. This is different from normal socket behavior because the empty string is a valid websocket message.</p> </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"><span class="pre">timeout</span></tt> – Universal Timeouts</a></p> <h4>Next topic</h4> <p class="topless"><a href="wsgi.html" title="next chapter"><tt class="docutils literal"><span class="pre">wsgi</span></tt> – 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" /> <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="../py-modindex.html" title="Python 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 0.13.0 documentation</a> »</li> <li><a href="../modules.html" >Module Reference</a> »</li> </ul> </div> <div class="footer"> © Copyright 2005-2010, Eventlet Contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>