Curl command from Python Subprocess to store output into variable
<p>I am using the below python code to read data from a url. The curl command from unix works. But when i try to store the returned json in a python variable, it is always blank.</p>

<p>Any pointers ? I do see the output on the Spyder Console, but never in the variable.</p>

<pre><code>    p =sp.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-H", "Authorization:Basic NEg0VU9QR1BZODAWVI4N1dLUFpXRzp4SVpxUUkzbUFuVG9RUlJDcXBLWkdB","-d", '{ "grant_type": "client_credentials" }', "https://rridata.wikimapia.com/v1.0/oauth/token/"], stdout = sp.PIPE, shell=False)
#p =sp.check_output(['curl','-i','-X', 'POST' ,'-H', 'Content-Type:application/json' ,'-H', 'Authorization:Basic NEg0VU9QR1BZODATEpDc2oyNGRGa0c5SVpxUUkzbUFuVG9RUlJDcXBLWkdB','-d', '{ "grant_type": "client_credentials" }', 'https://rdata.wikimapia.com/v1.0/oauth/token/'])
out,err = p.communicate()
print out
</code></pre>

<p>EDIT: My environment details. I am on Windows 7 , executing the command from Anaconda Spyder IDE.</p>

<p>You can use the subprocess PIPE to capture the stdout and stderr, like so:</p>

<pre><code>&gt;&gt;&gt; import subprocess
&gt;&gt;&gt; p = subprocess.Popen(["curl", "https://google.co.uk"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
&gt;&gt;&gt; print p.stdout.read()  # &lt;-- you can assign a variable to the content of stdout
&lt;HTML&gt;&lt;HEAD&gt;&lt;meta http-equiv="content-type" content="text/html;charset=utf-8"&gt;
&lt;TITLE&gt;301 Moved&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;
&lt;H1&gt;301 Moved&lt;/H1&gt;
The document has moved
&lt;A HREF="https://www.google.co.uk/"&gt;here&lt;/A&gt;.
&lt;/BODY&gt;&lt;/HTML&gt;
</code></pre>

python api curl pipe subprocess