Execute curl command within Python

19,583

Solution 1

Try this

import subprocess

bash_com = 'curl -k -H "Authorization: Bearer xxxxxxxxxxxxxxxx" -H "hawkular-tenant: test" -X GET https://www.example.com/test | python -m json.tool'
subprocess.Popen(bash_com)
output = subprocess.check_output(['bash','-c', bash_com])

This is a good way of doing this because it avoids using os.system which can make things ugly. But try to avoid calling bash commands from inside Python, specially in a case like this where you could simply use Requests instead.

Solution 2

You can use subprocess with Popen and communicate to execute commands and retrieve the output.

def executeCommand(cmd, debug = False):
   '''
   Excecute a command and return the stdiout and errors.
   cmd: list of the command. e.g.: ['ls', '-la']
   '''
   try:
      cmd_data = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      output,error = cmd_data.communicate()
      if debug:
         if (len(error)>1):
            print 'Error:', error
         if (len(output)>1):
            print 'Output:', output
      return output, error
   except:
      return 'Error in command:', cmd

Then, you put your command as

executeCommand(['curl', '-k', '-H', '"Authorization: Bearer xxxxxxxxxxxxxxxx"', '-H', '"hawkular-tenant: test"', '-X', 'GET', 'https://www.example.com/test', '|', 'python', '-m', 'json.tool'])
Share:
19,583

Related videos on Youtube

David Dimas
Author by

David Dimas

Updated on June 04, 2022

Comments

  • David Dimas
    David Dimas almost 2 years

    I am a beginner with python. I am trying to execute a curl command within Python script.

    If I do it in the terminal, it looks like this:

    curl -k -H "Authorization: Bearer xxxxxxxxxxxxxxxx" -H "hawkular-tenant: test" -X GET https://www.example.com/test | python -m json.tool
    

    I tried to do research, so I think I can use urllib2 library.

    How can I run this command?

    • Lukas Graf
      Lukas Graf about 8 years
      StackOverflow is not a cURL to Python requests conversion service. curl.trillworks.com is though.
    • DaveBensonPhillips
      DaveBensonPhillips about 8 years
      Is there a reason you have you use curl, rather than just sending HTTP requests from inside Python?
  • David Dimas
    David Dimas about 8 years
    [File "C:\Python27\lib\httplib.py", line 732, in _set_hostport raise InvalidURL("nonnumeric port: '%s'" % host[i+1:]) InvalidURL: nonnumeric port: '//hawkularmetrics.apps.10.2.2.2.xip.io/hawkular/metrics/met‌​rics'] I am getting this issue now.