1010
1111class UpgradableWSGIHandler (gevent .pywsgi .WSGIHandler ):
1212 """Upgradable version of gevent.pywsgi.WSGIHandler class
13-
13+
1414 This is a drop-in replacement for gevent.pywsgi.WSGIHandler that supports
1515 protocol upgrades via WSGI environment. This means you can create upgraders
1616 as WSGI apps or WSGI middleware.
17-
17+
1818 If an HTTP request comes in that includes the Upgrade header, it will add
1919 to the environment two items:
20-
20+
2121 ``upgrade.protocol``
2222 The protocol to upgrade to. Checking for this lets you know the request
23- wants to be upgraded and the WSGI server supports this interface.
24-
23+ wants to be upgraded and the WSGI server supports this interface.
24+
2525 ``upgrade.socket``
2626 The raw Python socket object for the connection. From this you can do any
2727 upgrade negotiation and hand it off to the proper protocol handler.
28-
28+
2929 The upgrade must be signalled by starting a response using the 101 status
3030 code. This will inform the server to flush the headers and response status
31- immediately, not to expect the normal WSGI app return value, and not to
32- look for more HTTP requests on this connection.
33-
31+ immediately, not to expect the normal WSGI app return value, and not to
32+ look for more HTTP requests on this connection.
33+
3434 To use this handler with gevent.pywsgi.WSGIServer, you can pass it to the
3535 constructor:
36-
36+
3737 .. code-block:: python
3838 :linenos:
3939
40- server = WSGIServer(('127.0.0.1', 80), app,
40+ server = WSGIServer(('127.0.0.1', 80), app,
4141 handler_class=UpgradableWSGIHandler)
42-
43- Alternatively, you can specify it as a class variable for a WSGIServer
42+
43+ Alternatively, you can specify it as a class variable for a WSGIServer
4444 subclass:
45-
45+
4646 .. code-block:: python
4747 :linenos:
4848
4949 class UpgradableWSGIServer(gevent.pywsgi.WSGIServer):
5050 handler_class = UpgradableWSGIHandler
51-
51+
5252 """
5353 def run_application (self ):
5454 upgrade_header = self .environ .get ('HTTP_UPGRADE' , '' ).lower ()
@@ -65,12 +65,12 @@ def start_response_for_upgrade(status, headers, exc_info=None):
6565 sline = '%s %s\r \n ' % (self .request_version , self .status )
6666 write (sline )
6767 self .response_length += len (sline )
68-
68+
6969 for header in headers :
7070 hline = '%s: %s\r \n ' % header
7171 write (hline )
7272 self .response_length += len (hline )
73-
73+
7474 write ('\r \n ' )
7575 self .response_length += 2
7676 else :
@@ -93,17 +93,17 @@ def start_response_for_upgrade(status, headers, exc_info=None):
9393
9494class WebSocketServer (gevent .pywsgi .WSGIServer ):
9595 handler_class = UpgradableWSGIHandler
96-
96+
9797 def __init__ (self , address , * args , ** kwargs ):
9898 protocols = kwargs .pop ('websocket_protocols' , [])
9999 extensions = kwargs .pop ('websocket_extensions' , [])
100100 websocket = kwargs .pop ('websocket_class' , WebSocket )
101-
101+
102102 gevent .pywsgi .WSGIServer .__init__ (self , address , * args , ** kwargs )
103103 self .application = WebSocketUpgradeMiddleware (app = self .handler ,
104104 protocols = protocols ,
105105 extensions = extensions ,
106- websocket_class = websocket )
106+ websocket_class = websocket )
107107
108108 def handler (self , websocket ):
109109 g = gevent .spawn (websocket .run )
@@ -112,7 +112,6 @@ def handler(self, websocket):
112112
113113if __name__ == '__main__' :
114114 import logging
115- import sys
116115 logging .basicConfig (format = '%(asctime)s %(message)s' )
117116 logger = logging .getLogger ()
118117 logger .setLevel (logging .DEBUG )
@@ -123,4 +122,3 @@ def handler(self, websocket):
123122 from ws4py .websocket import EchoWebSocket
124123 server = WebSocketServer (('127.0.0.1' , 9001 ), websocket_class = EchoWebSocket )
125124 server .serve_forever ()
126-
0 commit comments