0

I have this curl command that I want into python code command = ' curl -k -u test:123 -H "Content-type: application/json" --digest "https://192.168.0.10/restapi/config/minutes/"'

The command works fine in the terminal

I have tried two methods using pycurl and requests. Apparantely, none of them work. Let me share further on this:

Pycurl approach:

import pycurl
from io import StringIO
buffer = StringIO()
url = "https://192.168.0.10/restapi/config/minutes/"
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.SSL_VERIFYPEER, False)
curl.setopt(pycurl.USERPWD, 'test:123')
curl.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
curl.setopt(pycurl.VERBOSE, 1)
curl.perform()
resp = buffer.getvalue()

I get empty string as an output

Requests approach:

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

    response = requests.get(url, headers=headers, verify=False, auth=('test', '123'))

I get "Response [401]"

where am I going wrong? I know that subprocess is not a recomended option, but it is the only method that seems to work :

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
Mav
  • 123
  • 1
  • 8
  • 1
    Your `curl` specifies digest auth but your `requests` has `auth=('user','pw')` which does _basic_; try `requests.auth.HTTPDigestAuth('user','pw')` . For pycurl to write to `buffer` you need `curl.setopt(pycurl.WRITEDATA,buffer)` and if commandline needed to specify digest pycurl=libcurl probably does also. – dave_thompson_085 Nov 01 '19 at 23:33
  • Thanks Dave for the comment. requests.auth.HTTPDigestAuth worked smoothly. – Mav Nov 04 '19 at 13:29

0 Answers0