Created
September 26, 2016 10:24
-
-
Save glynnbird/89ad23c2f5e218d067f0de76150598e0 to your computer and use it in GitHub Desktop.
Python script to convert simple Redis commands to raw Redis protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
line
used in here: https://gist.github.com/glynnbird/89ad23c2f5e218d067f0de76150598e0#file-toredis-py-L21 is passed via environment (should be either passed viagen_redis_proto
signature or renamed tocmd
)