Skip to content

Commit 0b25ff4

Browse files
dbantymiguelgrinberg
authored andcommitted
Added rediss:// URL scheme to AsyncRedisManager (#319)
* Added rediss:// URL scheme to AsyncRedisManager * Obeyed flake8
1 parent 5e7c4df commit 0b25ff4

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

socketio/asyncio_redis_manager.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212

1313
def _parse_redis_url(url):
1414
p = urlparse(url)
15-
if p.scheme != 'redis':
15+
if p.scheme not in {'redis', 'rediss'}:
1616
raise ValueError('Invalid redis url')
17+
ssl = p.scheme == 'rediss'
1718
host = p.hostname or 'localhost'
1819
port = p.port or 6379
1920
password = p.password
2021
if p.path:
2122
db = int(p.path[1:])
2223
else:
2324
db = 0
24-
return host, port, password, db
25+
return host, port, password, db, ssl
2526

2627

2728
class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
@@ -39,7 +40,8 @@ class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
3940
'redis://hostname:port/0'))
4041
4142
:param url: The connection URL for the Redis server. For a default Redis
42-
store running on the same host, use ``redis://``.
43+
store running on the same host, use ``redis://``. To use an
44+
SSL connection, use ``rediss://``.
4345
:param channel: The channel name on which the server sends and receives
4446
notifications. Must be the same in all the servers.
4547
:param write_only: If set ot ``True``, only initialize to emit events. The
@@ -54,7 +56,9 @@ def __init__(self, url='redis://localhost:6379/0', channel='socketio',
5456
raise RuntimeError('Redis package is not installed '
5557
'(Run "pip install aioredis" in your '
5658
'virtualenv).')
57-
self.host, self.port, self.password, self.db = _parse_redis_url(url)
59+
(
60+
self.host, self.port, self.password, self.db, self.ssl
61+
) = _parse_redis_url(url)
5862
self.pub = None
5963
self.sub = None
6064
super().__init__(channel=channel, write_only=write_only, logger=logger)
@@ -66,7 +70,8 @@ async def _publish(self, data):
6670
if self.pub is None:
6771
self.pub = await aioredis.create_redis(
6872
(self.host, self.port), db=self.db,
69-
password=self.password)
73+
password=self.password, ssl=self.ssl
74+
)
7075
return await self.pub.publish(self.channel,
7176
pickle.dumps(data))
7277
except (aioredis.RedisError, OSError):
@@ -87,7 +92,8 @@ async def _listen(self):
8792
if self.sub is None:
8893
self.sub = await aioredis.create_redis(
8994
(self.host, self.port), db=self.db,
90-
password=self.password)
95+
password=self.password, ssl=self.ssl
96+
)
9197
self.ch = (await self.sub.subscribe(self.channel))[0]
9298
return await self.ch.get()
9399
except (aioredis.RedisError, OSError):

tests/asyncio/test_asyncio_redis_manager.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@
88
class TestAsyncRedisManager(unittest.TestCase):
99
def test_default_url(self):
1010
self.assertEqual(asyncio_redis_manager._parse_redis_url('redis://'),
11-
('localhost', 6379, None, 0))
11+
('localhost', 6379, None, 0, False))
1212

1313
def test_only_host_url(self):
1414
self.assertEqual(
1515
asyncio_redis_manager._parse_redis_url('redis://redis.host'),
16-
('redis.host', 6379, None, 0))
16+
('redis.host', 6379, None, 0, False))
1717

1818
def test_no_db_url(self):
1919
self.assertEqual(
2020
asyncio_redis_manager._parse_redis_url('redis://redis.host:123/1'),
21-
('redis.host', 123, None, 1))
21+
('redis.host', 123, None, 1, False))
2222

2323
def test_no_port_url(self):
2424
self.assertEqual(
2525
asyncio_redis_manager._parse_redis_url('redis://redis.host/1'),
26-
('redis.host', 6379, None, 1))
26+
('redis.host', 6379, None, 1, False))
2727

2828
def test_password(self):
2929
self.assertEqual(
3030
asyncio_redis_manager._parse_redis_url('redis://:pw@redis.host/1'),
31-
('redis.host', 6379, 'pw', 1))
31+
('redis.host', 6379, 'pw', 1, False))
3232

3333
def test_no_host_url(self):
3434
self.assertEqual(
3535
asyncio_redis_manager._parse_redis_url('redis://:123/1'),
36-
('localhost', 123, None, 1))
36+
('localhost', 123, None, 1, False))
3737

3838
def test_no_host_password_url(self):
3939
self.assertEqual(
4040
asyncio_redis_manager._parse_redis_url('redis://:pw@:123/1'),
41-
('localhost', 123, 'pw', 1))
41+
('localhost', 123, 'pw', 1, False))
4242

4343
def test_bad_port_url(self):
4444
self.assertRaises(ValueError, asyncio_redis_manager._parse_redis_url,
@@ -51,3 +51,9 @@ def test_bad_db_url(self):
5151
def test_bad_scheme_url(self):
5252
self.assertRaises(ValueError, asyncio_redis_manager._parse_redis_url,
5353
'http://redis.host:123/1')
54+
55+
def test_ssl_scheme(self):
56+
self.assertEqual(
57+
asyncio_redis_manager._parse_redis_url('rediss://'),
58+
('localhost', 6379, None, 0, True)
59+
)

0 commit comments

Comments
 (0)