Skip to content

Instantly share code, notes, and snippets.

@glynnbird
Created September 26, 2016 10:24
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save glynnbird/89ad23c2f5e218d067f0de76150598e0 to your computer and use it in GitHub Desktop.
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))
@mirekphd
Copy link

mirekphd commented Nov 21, 2022

line used in here: https://gist.github.com/glynnbird/89ad23c2f5e218d067f0de76150598e0#file-toredis-py-L21 is passed via environment (should be either passed via gen_redis_proto signature or renamed to cmd)

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