0

I have already gone through few StackOverflow existing links for this query, did not help me.

I would like to run few curl command(4) and each curl commands give output. From that output, I would like to parse the few group ids for next command.

curl --basic -u admin:admin -d \'{ "name" : "test-dev" }\' --header \'Content-Type: application/json\' http://localhost:8080/mmc/api/serverGroups

I have tried with as ,

#!/usr/bin/python

import subprocess
bash_com = 'curl --basic -u admin:admin -d '{ "name" : "test-dev" }' --header 'Content-Type: application/json' http://localhost:8080/mmc/api/serverGroups'
subprocess.Popen(bash_com)
output = subprocess.check_output(['bash','-c', bash_com]) # subprocess has check_output method

It gives me the syntax error, though I have changed from a single quote to double quote for that curl command.

I have been trying with Pycurl but i have to look more into that. Is there any way we can run curl commands in python and can parse the output values and pass it to next curl command.

12
  • What is the syntax error? Commented Oct 5, 2016 at 11:09
  • It gives syntax error to admin:admin Commented Oct 5, 2016 at 11:10
  • # python -x api.py File "api.py", line 4 bash_com = curl --basic -u admin:admin -d '{ "name" : "test-dev" }' --header 'Content-Type: application/json' localhost:8080/mmc/api/serverGroups ^ SyntaxError: invalid syntax Commented Oct 5, 2016 at 11:11
  • Use triple quotes: bash_com = """curl --basic -u admin:admin -d '{ "name" : "test-dev" }' --header 'Content-Type: application/json' http://localhost:8080/mmc/api/serverGroups""". Commented Oct 5, 2016 at 11:11
  • 1
    as I know Popen expects list ['curl', '--basic', ...] Commented Oct 5, 2016 at 11:20

3 Answers 3

1

You can use os.popen with

fh = os.popen(bash_com, 'r')
data = fh.read()
fh.close()

Or you can use subprocess like this

cmds = ['ls', '-l', ]

try:
    output = subprocess.check_output(cmds, stderr=subprocess.STDOUT)
    retcode = 0
except subprocess.CalledProcessError, e:
    retcode = e.returncode
    output = e.output

print output

There you have to organize your command and params in a list.

Or you just go the easy way and use requests.get(...).

And do not forget: Using popen you can get shell injections via parameters of your command!

Sign up to request clarification or add additional context in comments.

Comments

0

Better output using os.open(bash_com,'r') and then fh.read()

python api.py

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 199 172 0 172 0 27 3948 619 --:--:-- --:--:-- --:--:-- 4027 {"href":"http://localhost:8080/mmc/api/serverGroups/39a28908-3fae-4903-adb5-06a3b7bb06d8","serverCount":0,"name":"test-dev","id":"39a28908-3fae-4903-adb5-06a3b7bb06d8"}

trying understand that fh.read() has executed the curl command? please correct me

Comments

0

I am trying to redirect the curl commands output to text file and then parse the file via JSON. All I am trying to get "id" from about output.

fh = os.popen(bash_com,'r')
data = fh.read()

newf = open("/var/tmp/t1.txt",'w')
sys.stdout = newf
print data

with open("/var/tmp/t1.txt") as json_data:
    j = json.load(json_data)
    print j['id']

I have checked the files content in JSONlint.com and got VALID JSON on it. It is throwing "ValueError: No JSON object could be decoded" at json.load line. Is there anything need to perform before parsing the redirected file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.