2

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?

2
  • StackOverflow is not a cURL to Python requests conversion service. curl.trillworks.com is though. Commented Mar 11, 2016 at 19:12
  • Is there a reason you have you use curl, rather than just sending HTTP requests from inside Python? Commented Mar 11, 2016 at 19:12

4 Answers 4

3

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.

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

Comments

1

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'])

Comments

0

I wouldn't recommend calling curl via your shell from inside Python. What about httplib?

import httplib
conn = httplib.HTTPConnection("https://www.example.com/test")
conn.request("HEAD","Authorization: Bearer xxxxxxxxxxxxxxxx")
conn.request("HEAD", "hawkular-tenant: test")
res = conn.getresponse()

If you're using Python3 then you'll need to swap httplib for http.client

1 Comment

[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/metrics'] I am getting this issue now.
-1

You can use the pycurl library.

pip install pycurl

Instructions and examples here

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.