Jeffrey Gelens committed 46cbf6a

Fixed bugs

  • Participants
  • Parent commits c96f90c

Comments (0)

Files changed (4)

File examples/

 import os
-from gevent.pywsgi import WSGIServer
 import geventwebsocket
+from geventwebsocket.server import WebSocketServer
-def echo(environ, start_response):
+def echo_app(environ, start_response):
     websocket = environ.get("wsgi.websocket")
     if websocket is None:
         return http_handler(environ, start_response)
     except geventwebsocket.WebSocketError, ex:
-        print "%s: %s" % (ex.__class__.__name__, ex)
+        print "{0}: {1}".format(ex.__class__.__name__, ex)
 def http_handler(environ, start_response):
     if environ["PATH_INFO"].strip("/") == "version":
         start_response("200 OK", [])
         return [agent]
         start_response("400 Bad Request", [])
         return ["WebSocket connection is expected here."]
 path = os.path.dirname(geventwebsocket.__file__)
 agent = "gevent-websocket/%s" % (geventwebsocket.__version__)
 print "Running %s from %s" % (agent, path)
-WSGIServer(("", 8000), echo, handler_class=geventwebsocket.WebSocketHandler).serve_forever()
+WebSocketServer(("", 8000), echo_app, debug=True).serve_forever()

File geventwebsocket/

 from gevent.pywsgi import WSGIHandler
 from .websocket import WebSocket, Stream
+from .logging import create_logger
 class WebSocketHandler(WSGIHandler):
             connection = self.environ.get('HTTP_CONNECTION', '').lower()
             if 'upgrade' not in connection:
-                # this is not a websocket request, so we must not handle it
+                # This is not a websocket request, so we must not handle it
                 self.logger.warning("Client didn't ask for a connection "
+        else:
+            # This is not a websocket request, so we must not handle it
+            return
         if self.environ.get('HTTP_SEC_WEBSOCKET_VERSION'):
             return self.upgrade_connection()
+            self.logger.warning("No protocol defined")
             self.start_response('426 Upgrade Required', [
                 ('Sec-WebSocket-Version', ', '.join(self.SUPPORTED_VERSIONS))])
-            self.logger.warning("No protocol defined")
             return ['No Websocket protocol version defined']
     def logger(self):
+        if not hasattr(self.server, 'logger'):
+            self.server.logger = create_logger(__name__)
         return self.server.logger
 #class MessageHandler(object):
 #    def __init__(self, environ, interfaces):
 # = environ['wsgi.websocket']

File geventwebsocket/

         return text.encode('utf-8')
-    def is_valid_close_code(self, code):
+    def _is_valid_close_code(self, code):
         :returns: Whether the returned close code is a valid hybi return code.
         :return: The header and payload as a tuple.
         header = Header.decode_header(
         if header.flags:
         except ProtocolError:
-            raise
         except error:
             raise WebSocketError("Socket is dead")
                 struct.pack('!H%ds' % len(message), code, message),
         except WebSocketError:
-            # failed to write the closing frame but it's ok because we're
+            # Failed to write the closing frame but it's ok because we're
             # closing the socket anyway.

File tests/

 class ProcessPool(object):
     def __init__(self):
         self.popens = []