Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess.Popen not escaping command line arguments properly?

I am trying to call the following curl command with python:

curl -k -F [email protected] -F "data={\\"title\\":\\"Another App\\"}" -Lu usr:pwd https://build.phonegap.com/api/v0/apps

For it to work, I've found that the json I'm passing in data needs to be escaped with backslashes.

I can call this command with...

os.system(curl -k -F [email protected] -F "data={\\"title\\":\\"Another App\\"}" -Lu usr:pwd https://build.phonegap.com/api/v0/apps)

and it works.

However, when I try to use the subprocess module like this...

s = 'curl -k -F [email protected] -F "data={\\"title\\":\\"Another App\\"}" -Lu usr:pwd https://build.phonegap.com/api/v0/apps'
push = subprocess.Popen(s.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, errors = push.communicate()
print output

...the curl doesn't work and I get an error from the api I'm using that I'm using invalid parameters, which I've gotten in the past when I've used improperly escaped json.

What is going on here? Why can I call this command with os.system and not subprocess.Popen? So far my hypothesis is that the split is messing up something in the string, but I didn't find anything that looked wrong when I check the output of s.split().

like image 742
deeb Avatar asked Dec 17 '22 11:12

deeb


2 Answers

perhaps using shell=True

push = subprocess.Popen(s, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
like image 74
fransua Avatar answered Mar 03 '23 23:03

fransua


Instead of doing

s.split()

try using shlex from the standard library

import shlex
shlex.split(s)

Shlex allows you configure the escaping behavior (see the link for details, the defaults might be sufficient though)

like image 40
redacted Avatar answered Mar 03 '23 22:03

redacted