Applescript to Python

Posted on
Wed Apr 18, 2018 2:44 pm
Sam offline
Posts: 34
Joined: Aug 22, 2016

Applescript to Python

Hi,
I hope someone can help me convert this Applescript to Python. It is used to enable an output of an intrusion alarm.

Code: Select all
do shell script "curl https://192.168.10.18:8088/spc/output/1/set -X PUT -u Username:Password --digest"


Thanks in advance.

/Sam

Posted on
Wed Apr 18, 2018 3:31 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Applescript to Python

You can always throw that into an AppleScript file and run it with the plugin I wrote for that purpose or you can do this:

Code: Select all
import requests
url = 'https://192.168.10.18:8088/spc/output/1/set'
response = requests.post(url,auth=HttpNtlmAuth('username','Password'))

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Wed Apr 18, 2018 4:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python

Colorado4Wheeler wrote:
You can always throw that into an AppleScript file and run it with the plugin I wrote for that purpose or you can do this:

Code: Select all
import requests
url = 'https://192.168.10.18:8088/spc/output/1/set'
response = requests.post(url,auth=HttpNtlmAuth('username','Password'))



So, strictly speaking, this is a direct port from curl:

Code: Select all
import requests
url = 'https://192.168.10.18:8088/spc/output/1/set'
response = requests.put(url,auth=requests.auth.HttpDigestAuth('username','Password'))


Except that there is no data being put. Maybe that's OK, but normally you pass in some data (the original curl doesn't have any either).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Apr 18, 2018 4:35 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Applescript to Python

jay (support) wrote:
Except that there is no data being put. Maybe that's OK

That was my thought when I wrote that, but it's 1:1.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Wed Apr 18, 2018 4:50 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python

Colorado4Wheeler wrote:
jay (support) wrote:
Except that there is no data being put. Maybe that's OK

That was my thought when I wrote that, but it's 1:1.


I don't recognize HttpNtlmAuth - what's that?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Apr 18, 2018 4:57 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Applescript to Python

Ahh, oops, that was in my head from something else I was working on in python outside of Indigo, it's part of request_ntlm for using Windows authentication. That's what I get for coding off the top of my head rather than pulling up code from another project :roll:.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Wed Apr 18, 2018 10:57 pm
Sam offline
Posts: 34
Joined: Aug 22, 2016

Re: Applescript to Python

Thank you for your help guys!
I know nothing about coding and python but when I'm execute the script:
Code: Select all
import requests
url = 'https://192.168.10.18:8088/spc/output/1/set'
response = requests.put(url,auth=requests.auth.HttpDigestAuth('username','Password'))

I get an error: embedded script, line 3, at top level
AttributeError: 'module' object has no attribute 'HttpDigestAuth'

What am I doing wrong?

/Sam

Posted on
Thu Apr 19, 2018 4:08 am
CliveS offline
Posts: 770
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: Applescript to Python

Having dabbled with Python (and spent many an hour over False and false) Python is case sensitive and all examples I looked at use HTTPDigestAuth so try

Code: Select all
import requests
url = 'https://192.168.10.18:8088/spc/output/1/set'
response = requests.put(url,auth=requests.auth.HTTPDigestAuth('username','Password'))

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Thu Apr 19, 2018 5:41 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Applescript to Python

I think Clive is right. From the docs:

Code: Select all
requests.get(url, auth=HTTPDigestAuth('user', 'pass'))  # or requests.put(url, auth=HTTPDigestAuth('user', 'pass'))

http://docs.python-requests.org/en/master/user/authentication/#digest-authentication

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

[My Plugins] - [My Forums]

Posted on
Thu Apr 19, 2018 6:16 am
Sam offline
Posts: 34
Joined: Aug 22, 2016

Re: Applescript to Python

Thank you very much CliveS and Dave.
Works perfect!

/Sam

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests