Commits

Jeffrey Gelens committed 46cbf6a

Fixed bugs

Comments (0)

Files changed (4)

examples/echoserver.py

 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)
     try:
                 break
             websocket.send(message)
         websocket.close()
+
     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]
+
     else:
         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()

geventwebsocket/handler.py

 
 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 "
                                     "upgrade")
                 return
+        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()
         else:
+            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']
 
 
     @property
     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):
 #        self.ws = environ['wsgi.websocket']

geventwebsocket/websocket.py

 
         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(self.stream)
 
         if header.flags:
 
         except ProtocolError:
             self.close(1002)
-            raise
 
         except error:
             raise WebSocketError("Socket is dead")
                 struct.pack('!H%ds' % len(message), code, message),
                 opcode=self.OPCODE_CLOSE)
         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.
             pass
         finally:

tests/run_autobahn_tests.py

 
 
 class ProcessPool(object):
-
     def __init__(self):
         self.popens = []
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.