1616
1717"""
1818import logging
19- import sys
2019
2120import gevent
2221from gevent .pywsgi import WSGIHandler , WSGIServer as _WSGIServer
2524from ws4py import format_addresses
2625from ws4py .server .wsgiutils import WebSocketWSGIApplication
2726
27+
2828logger = logging .getLogger ('ws4py' )
2929
3030__all__ = ['WebSocketWSGIHandler' , 'WSGIServer' ,
3131 'GEventWebSocketPool' ]
3232
33+
3334class WebSocketWSGIHandler (WSGIHandler ):
3435 """
3536 A WSGI handler that will perform the :rfc:`6455`
@@ -43,24 +44,21 @@ class WebSocketWSGIHandler(WSGIHandler):
4344 def run_application (self ):
4445 upgrade_header = self .environ .get ('HTTP_UPGRADE' , '' ).lower ()
4546 if upgrade_header :
46- try :
47- # Build and start the HTTP response
48- self .environ ['ws4py.socket' ] = self .socket or self .environ ['wsgi.input' ].rfile ._sock
49- self .result = self .application (self .environ , self .start_response ) or []
50- self .process_result ()
51- except :
52- raise
53- else :
54- del self .environ ['ws4py.socket' ]
55- self .socket = None
56- self .rfile .close ()
57-
58- ws = self .environ .pop ('ws4py.websocket' , None )
59- if ws :
60- self .server .pool .track (ws )
47+ # Build and start the HTTP response
48+ self .environ ['ws4py.socket' ] = self .socket or self .environ ['wsgi.input' ].rfile ._sock
49+ self .result = self .application (self .environ , self .start_response ) or []
50+ self .process_result ()
51+ del self .environ ['ws4py.socket' ]
52+ self .socket = None
53+ self .rfile .close ()
54+
55+ ws = self .environ .pop ('ws4py.websocket' , None )
56+ if ws :
57+ self .server .pool .track (ws )
6158 else :
6259 gevent .pywsgi .WSGIHandler .run_application (self )
6360
61+
6462class GEventWebSocketPool (Pool ):
6563 """
6664 Simple pool of bound websockets.
@@ -85,7 +83,8 @@ def clear(self):
8583 pass
8684 finally :
8785 self .discard (greenlet )
88-
86+
87+
8988class WSGIServer (_WSGIServer ):
9089 handler_class = WebSocketWSGIHandler
9190
@@ -106,8 +105,8 @@ def stop(self, *args, **kwargs):
106105 self .pool .clear ()
107106 _WSGIServer .stop (self , * args , ** kwargs )
108107
108+
109109if __name__ == '__main__' :
110- import os
111110
112111 from ws4py import configure_logger
113112 configure_logger ()
0 commit comments