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?
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.