Skip to content

Instantly share code, notes, and snippets.

View Alex-Addy's full-sized avatar

Alex Addy Alex-Addy

View GitHub Profile
@Alex-Addy
Alex-Addy / gen_redis_protocol.py
Created April 17, 2017 23:07
Generate redis protocol in python 2 and 3
def gen_redis_proto(*args):
proto = ''
proto += '*' + str(len(args)) + '\r\n'
for arg in args:
# Pick one of the two below lines and comment out the other.
# Python 2
proto += '$' + str(len(arg)) + '\r\n'
# Due to python 3's string changes len no longer returns the number of bytes
# in a string and instead returns the number of characters. The redis protocol
# needs the number of bytes. In order to get the bytes we coerce it into a byte