0

I am new to Python. I am trying to convert below bash script command to Python using subprocess. I don't get any output nor do I see any failure when I execute my Python script.

This is the bash command that needs to be ported to Python.

curl -u 'lawn:oldlawn!' -k -s -H 'Content-Type: application/json' -X GET 'https://192.168.135.20:443/api/json/v2/types/dev-objs/1'

My python code:

get_curl():
  curl = """curl -u 'lawn:oldlawn!' -k -s -H 'Content-Type: application/json' -X GET 'https://192.168.135.20:443/api/json/v2/types/dev-objs/1'"""

  args = curl.split()

  process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

  stdout, stderr = process.communicate()
  print stdout
  print stderr

# End of python code

After I execute get_curl(), print stdout and print stderr does not print anything though dev-objs/1 exists.

When I execute the same command as bash command it works and I see the REST API output.

Can anyone help me what may be going wrong here? Thanks

8
  • to convert string to list of arguments try shlex.split, not str.split. Commented May 24, 2016 at 21:58
  • Just curious; is there a reason why you would want to execute curl instead of using urllib? Commented May 24, 2016 at 22:01
  • bash is awful and subprocess is not great either. Use the requests library instead of curl (and instead of urllib too). Commented May 24, 2016 at 22:02
  • @AlexHall: Whyever would you recommend an external, third-party library for such simple HTTP calls when urllib is built in? Commented May 24, 2016 at 22:03
  • 1
    @Dolda2000 gist.github.com/kennethreitz/973705 Commented May 24, 2016 at 22:06

2 Answers 2

3

You could use requests:

headers = {
    'Content-Type': 'application/json',
}


import requests

proxies = {
  'https': 'https://lawn:[email protected]:443',
}

req = requests.get('https:...', proxies=proxies, verify=False, headers=headers)

I don't think there is a -s flag for requests as no data will be output unless you print what is returned with req.json().

I thought you had a capital U not a u so all you really need is to do is to use basic-authentication passing auth = (user, pass), set verify=False as per Brendans answer.

Why your subprocess code did not work was because you have some args quoted like "'lawn:oldlawn!'", what you want is:

args = ['curl',
 '-u',
 'lawn:oldlawn!',
 '-k',
 '-s',
 '-H',
 'Content-Type: application/json',
 '-X',
 'GET',
 'https://192.168.135.20:443/api/json/v2/types/dev-objs/1']

process = subprocess.Popen(args, shell=False,   stdout=subprocess.PIPE, stderr=subprocess.PIPE)

stdout, stderr = process.communicate()
Sign up to request clarification or add additional context in comments.

4 Comments

I was looking for the requests syntax, and when I came back you had posted this :P (also note, that it's probably better to set up the SSL cert as the CA root or something rather than forget about verification)
What is the purpose of using the proxy?
@WayneWerner, yep that would definitely be a better approach, the OP used -k so I just replicated.
Natch :) I just wanted to offer that bit of enhancement for anyone who comes looking. I'm not even sure if curl can do that. Or maybe just stay lazy and use verify=False myself :P
1

Using requests will make your http(s) requests far simpler:

import requests


response = requests.get('https://192.168.135.20:443/api/json/v2/types/dev-objs/1', 
                        verify=False, auth=('lawn', 'oldlawn!'))
print response.json()

3 Comments

You need a verify=False on here
Thank you all, using shelx.split() resolved my issue.
Next I will convert this to use request library rather than subprocess.

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.