Skip to content

Instantly share code, notes, and snippets.

@laserson
Created May 13, 2012 18:56
Show Gist options
  • Save laserson/2689744 to your computer and use it in GitHub Desktop.
Save laserson/2689744 to your computer and use it in GitHub Desktop.
Generate Redis protocol with python
def gen_redis_proto(*args):
proto = ''
proto += '*' + str(len(args)) + '\r\n'
for arg in args:
proto += '$' + str(len(arg)) + '\r\n'
proto += str(arg) + '\r\n'
return proto
@Alex-Addy
Copy link

In python3 this will result in an incorrect length for unicode strings. Changing line 5 to

proto += '$' + str(len(bytes(str(arg), 'utf-8'))) + '\r\n'

worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment