From 2e71c22c6db41b82d863b3189c122ea9f7916bf0 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 12 Dec 2020 14:59:30 +0000 Subject: [PATCH] Return environ dictionary for a client --- socketio/server.py | 10 ++++++++++ tests/common/test_server.py | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/socketio/server.py b/socketio/server.py index c2c4969..18b2188 100644 --- a/socketio/server.py +++ b/socketio/server.py @@ -534,6 +534,16 @@ class Server(object): """ return self.eio.transport(sid) + def get_environ(self, sid, namespace=None): + """Return the WSGI environ dictionary for a client. + + :param sid: The session of the client. + :param namespace: The Socket.IO namespace. If this argument is omitted + the default namespace is used. + """ + eio_sid = self.manager.eio_sid_from_sid(sid, namespace or '/') + return self.environ.get(eio_sid) + def handle_request(self, environ, start_response): """Handle an HTTP request from the client. diff --git a/tests/common/test_server.py b/tests/common/test_server.py index d8221e3..0732bc3 100644 --- a/tests/common/test_server.py +++ b/tests/common/test_server.py @@ -746,6 +746,14 @@ class TestServer(unittest.TestCase): with pytest.raises(ValueError): s.register_namespace(AsyncNS()) + def test_get_environ(self, eio): + s = server.Server() + s._handle_eio_connect('123', 'environ') + s._handle_eio_message('123', '0') + sid = s.manager.sid_from_eio_sid('123', '/') + assert s.get_environ(sid) == 'environ' + assert s.get_environ('foo') is None + def test_logger(self, eio): s = server.Server(logger=False) assert s.logger.getEffectiveLevel() == logging.ERROR