Update variable in Domoticz using Python JSON and HTTPS

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
koekerdt
Posts: 3
Joined: Saturday 05 November 2016 17:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Update variable in Domoticz using Python JSON and HTTPS

Post by koekerdt »

Hi all,

some time ago I've created a script to retrieve the sunset time from domoticz using JSON, apply some offset and then store the updated time again in domoticz using JSON. I'm using the NGINX webserver and redirect all traffic to HTTPS (as described in https://www.domoticz.com/wiki/Secure_Nginx_Proxy_Setup).

My script used to work just fine until recently I noticed it wasn't updating the variable anymore. The NGINX configuration and script have not been changed. Domoticz and the rPI software however have been update to the latest version (unfortunately I can't check whether the script was already failing before the update).

Retrieving the SunRIse still works fine using the functions below:

Code: Select all

def DomoticzGetSunrise ():
    JSON_getSunRise = '/json.htm?type=command&param=getSunRiseSet'
    domoticzurl = domoticzserver+JSON_getSunRise
    json_object = json.loads(domoticzrequest(domoticzurl))
      
    if json_object["status"] == "OK":
        result = json_object["Sunset"]
        return result
        
    return 0

Code: Select all

def domoticzrequest (url):    
    request = urllib2.Request(url)
    request.add_header("Authorization", "Basic %s" % base64string)
    response = urllib2.urlopen(request)
    print 'response headers: "%s"' % response.info()
    
    return response.read()
However updating the variable in Domoticz no longer works. The function used for updating Domoticz is:

Code: Select all

def DomoticzSetVariable (USERVARIABLENAME, USERVARIABLETYPE, USERVARIABLEVALUE):
    JSON_setVariable = '/json.htm?type=command&param=updateuservariable&vname='+USERVARIABLENAME+'&vtype='+USERVARIABLETYPE+'&vvalue='+USERVARIABLEVALUE
    domoticzurl = domoticzserver+JSON_setVariable    
    json_object = json.loads(domoticzrequest(domoticzurl))
    
    if json_object["status"] == "OK":
        return 1
    
    return 0
It seems that domoticzrequest() does not return (an usable) JSON object
ValueError: No JSON object could be decoded


I did some analysis and the part I find interesting is that the JSON request created as a result of the above functions works perfectly fine when manually entered in the webbrowsers address bar.

Anyone some idea what might be going on? I think it has something to do with using HTTPS (iso HTTP) and the recent update.

Any help is appreciated :)
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: Update variable in Domoticz using Python JSON and HTTPS

Post by stlaha2007 »

Not sure what happens here, as you said tested some stuff already...

Instead of pasting the generated url into the browser...
How does it actually look like. And have you tried it with curl on the commandline.

Can you post the results?

Sent from my D6603 using Tapatalk
koekerdt
Posts: 3
Joined: Saturday 05 November 2016 17:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Update variable in Domoticz using Python JSON and HTTPS

Post by koekerdt »

Passing the link to Curl results in the following:
<html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html>


This kinda makes sense since the urlib2.urlopen is not using an URL as input but a request object.
The request object is created by the urlib2.request(my domoticz JSON url) and a header with authorization is added by request.add_header("Authorization", "Basic %s" % base64string).

The funny part is that another script of mine (using the same functions but then to update a virtual switch instead of a variable) is still working fine.
Another interesting part is that after I logged in on Domoticz (using the same credentials as used in the script) the problem seems to be (temporary?) solved.

I find this strange behavior...
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: Update variable in Domoticz using Python JSON and HTTPS

Post by stlaha2007 »

If you login, theres a cookie taking over further authentication.

Just saying, probably not the correct words, but to indicate whats happing. As i also had the same issue some time ago.

As i couldn't get curl to authenticate correctly, I solved it to specify device-ranges [as i have several VLANs and ip-ranges] from where unauthenticated traffic is allowed in settings.

Eg. my metering-RPis in 192.168.234.xxx/24 and Tablets with Dashboard or Custom Frontpage 192.168.123.xxx/24

My servers and clients also can enter, however when i'm logged in through vpn i must supply credentials.

Sent from my D6603 using Tapatalk
koekerdt
Posts: 3
Joined: Saturday 05 November 2016 17:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Update variable in Domoticz using Python JSON and HTTPS

Post by koekerdt »

I don't think it's a cookie; I'm logging in on another device then the one running the script. Anyway for now it seems to work again and since this is not a vital functionality for my system to work, I'ts fine for now :)

Thanks for your responses!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest