Skip to content

Commit 0ed2f0c

Browse files
author
CM Lubinski
committed
Most of these strings are bytes
1 parent efd20ff commit 0ed2f0c

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

django_elasticache/cluster_utils.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
utils for discovery cluster
33
"""
44
from distutils.version import StrictVersion
5+
from django.utils.encoding import smart_text
56
import re
67
from telnetlib import Telnet
78

@@ -28,20 +29,20 @@ def get_cluster_info(host, port):
2829
}
2930
"""
3031
client = Telnet(host, int(port))
31-
client.write('version\n')
32-
res = client.read_until('\r\n').strip()
33-
version_list = res.split(' ')
34-
if len(version_list) != 2 or version_list[0] != 'VERSION':
32+
client.write(b'version\n')
33+
res = client.read_until(b'\r\n').strip()
34+
version_list = res.split(b' ')
35+
if len(version_list) != 2 or version_list[0] != b'VERSION':
3536
raise WrongProtocolData('version', res)
3637
version = version_list[1]
37-
if StrictVersion(version) >= StrictVersion('1.4.14'):
38-
cmd = 'config get cluster\n'
38+
if StrictVersion(smart_text(version)) >= StrictVersion('1.4.14'):
39+
cmd = b'config get cluster\n'
3940
else:
40-
cmd = 'get AmazonElastiCache:cluster\n'
41+
cmd = b'get AmazonElastiCache:cluster\n'
4142
client.write(cmd)
42-
res = client.read_until('\n\r\nEND\r\n')
43+
res = client.read_until(b'\n\r\nEND\r\n')
4344
client.close()
44-
ls = list(filter(None, re.compile(r'\r?\n').split(res)))
45+
ls = list(filter(None, re.compile(br'\r?\n').split(res)))
4546
if len(ls) != 4:
4647
raise WrongProtocolData(cmd, res)
4748

@@ -51,9 +52,10 @@ def get_cluster_info(host, port):
5152
raise WrongProtocolData(cmd, res)
5253
nodes = []
5354
try:
54-
for node in ls[2].split(' '):
55-
host, ip, port = node.split('|')
56-
nodes.append('{}:{}'.format(ip or host, port))
55+
for node in ls[2].split(b' '):
56+
host, ip, port = node.split(b'|')
57+
nodes.append('{}:{}'.format(smart_text(ip or host),
58+
smart_text(port)))
5759
except ValueError:
5860
raise WrongProtocolData(cmd, res)
5961
return {

tests/test_protocol.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010

1111
TEST_PROTOCOL_1 = [
12-
'VERSION 1.4.14',
13-
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
12+
b'VERSION 1.4.14',
13+
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
1414
]
1515

1616
TEST_PROTOCOL_2 = [
17-
'VERSION 1.4.13',
18-
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
17+
b'VERSION 1.4.13',
18+
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
1919
]
2020

2121

@@ -40,8 +40,8 @@ def test_last_versions(Telnet):
4040
client.read_until.side_effect = TEST_PROTOCOL_1
4141
get_cluster_info('', 0)
4242
client.write.assert_has_calls([
43-
call('version\n'),
44-
call('config get cluster\n'),
43+
call(b'version\n'),
44+
call(b'config get cluster\n'),
4545
])
4646

4747

@@ -51,6 +51,6 @@ def test_prev_versions(Telnet):
5151
client.read_until.side_effect = TEST_PROTOCOL_2
5252
get_cluster_info('', 0)
5353
client.write.assert_has_calls([
54-
call('version\n'),
55-
call('get AmazonElastiCache:cluster\n'),
54+
call(b'version\n'),
55+
call(b'get AmazonElastiCache:cluster\n'),
5656
])

0 commit comments

Comments
 (0)