How to get the response of calling a redis command
<p>I'm using python 2.6.6 and I can't upgrade.</p>

<p>I had it working fine with subprocess.check_output but I didn't realize we are using python 2.6.6 and I can't upgrade it on my end.</p>

<p>I tried this:</p>

<pre><code>command = "redis-cli hget some_key some_field"
command_output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
</code></pre>

<p>But I don't think I am passing in the commands correctly, the docs have somethign like:</p>

<pre><code>subprocess.Popen( ['ls', 'li'], ..)
</code></pre>

<p>Yup, you should use lists to give your command line.</p>

<pre><code>command = ["redis-cli", "hget", "some_key", "some_field"]
command_output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
</code></pre>

<p>Relevant from Moses Koledoye@</p>

<blockquote>
  <p><code>command.split()</code> does the conversion to list.</p>
</blockquote>

<p>The reason they did this is pretty simple: every argument given will be directly forward to the <code>redis-cli</code> program. Let's go in details:</p>

<p>Imagine you have this:</p>

<pre><code>key = raw_input("Please give me the key:")
os.system('redis-cli bla bla %s' % key)  # DON'T DO THIS AT HOME.
</code></pre>

<p>Now imagine I am a malicious guy, and I input the following: <code>|| echo "hacked by h4x0r"</code>. The final command will look like this :</p>

<pre><code>redis-cli bla bla || echo "hacked by h4x0r"
</code></pre>

<p>Use list. Really.</p>

python