How to do a cURL call from Python

Posted on
Mon Jan 03, 2022 6:02 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

How to do a cURL call from Python

I'm a seasoned Indigo user, but a Python semi-newbie. Python seems "different" on the Mac (I'm used to Linux), so I'm asking for a specific command, but I bet general advice would help.

In linux I can say
Code: Select all
curl --digest -u admin:{my_password} -X PUT -H "X-CSRF: x" --data "value=false" "http://10.0.2.14/restapi/relay/outlets/3/state/"


.. and the server at 10.0.2.14 comes back fine. However, when I do this from the Mac (10.0.2.12) that Indigo runs on (MacOX 12.0.1), the server at 10.0.2.14 times out with an internal error - it's not a TCP type error. I suspect it's something funky on the Mac, so I thought I would try and write a python script to simulate the cURL call.

Anyone have some pointers in how I would write the Python code? I know how to pass in data, but I don't know about the credentials digest, etc, and I want to do it the "indigo way".

Thanks,

Dave

Posted on
Mon Jan 03, 2022 6:22 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: How to do a cURL call from Python

Python is pretty much the same everywhere. Your issue is likely that you need to specify a full path to curl.

I'd recommend using the requests library (which we include with Indigo) directly in Python rather than calling out to curl.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jan 03, 2022 7:03 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: How to do a cURL call from Python

Thanks for the quick response Jay!

Perhaps my Mac (a standard install) is running an old version of Python? I know the 2.x versions are "retiring".

Code: Select all
dstrickler@Indigo-Foxtrot-Mac-Mini indigo % python

WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.18 (default, Oct  2 2021, 04:20:39)
[GCC Apple LLVM 13.0.0 (clang-1300.0.29.1) [+internal-os, ptrauth-isa=deploymen on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named requests
>>> quit()


Or with Python3 ...

Code: Select all
  dstrickler@Indigo-Foxtrot-Mac-Mini indigo % /usr/bin/python3
Python 3.8.9 (default, Jul 19 2021, 09:37:32)
[Clang 13.0.0 (clang-1300.0.27.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'requests'
>>> quit() 

Dave

Posted on
Mon Jan 03, 2022 8:08 pm
DaveL17 offline
User avatar
Posts: 6754
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: How to do a cURL call from Python

Requests is included with Indigo, but not with Python. You're going to have to import it from within an Indigo shell (or embedded script or linked script) or else you're going to need to install it yourself. If what you're doing is related to your Indigo setup--which it appears to be--I'd recommend running a linked script. Linked scripts aren't subject to the 10-second time limit that Indigo enforces on embedded scripts (linked scripts run in their own thread).

And I completely agree with Jay's recommendation to use Requests over curl whenever possible.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jan 04, 2022 9:22 am
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: How to do a cURL call from Python

Ah, so this matches what I see. If I use Python on the CLI, it doesn't have Requests. I need to run this from _within_ Indigo.

Thanks for the tip!

Dave

Posted on
Tue Jan 04, 2022 9:42 am
DaveL17 offline
User avatar
Posts: 6754
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: How to do a cURL call from Python

Glad to help. Good luck!

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Jan 05, 2022 8:12 am
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: How to do a cURL call from Python

If anyone needs the basic code, here it is. This runs from within Indigo, but as @daveL17 points out, it's smarter to make an external script and have Indigo run the script for timeout reasons. If you don't, Indigo will pause doing anything else while you're script runs - normally not a good idea.

This calls the API's url and logs out the results, assuming they are in JSON format (most APIs use that these days).

Code: Select all
import requests
import json

indigo.server.log("Making the API call...")

url = "https://you_domain.com/you_api/your_function"
resultObj = requests.get(url)

indigo.server.log("API call finished. resultObj:")
indigo.server.log(resultObj.text)

Dave

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests