Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Python script to convert simple Redis commands to raw Redis protocol
#!/usr/bin/python
# reads a sequence of REDIS commands from stdin e.g.
# SET mykey "hello world
# into Redis protocol e.g.
# *3
# $3
# SET
# $5
# mykey
# $5
# hello
#
# Usage:
# cat commands.txt | ./toredis.py | redis-cli --pipe
import sys
def gen_redis_proto(cmd):
# break the command by spaces to get the number of tokens
tokens = line.split(" ")
proto = "*" + str(len(tokens)) + "\r\n"
for token in tokens:
proto += "$" + str(len(token)) + "\r\n"
proto += token + "\r\n"
return proto
for line in sys.stdin:
line = line.strip()
sys.stdout.write(gen_redis_proto(line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment