Page 1 of 1

Python plugin: Help with http connection please

Posted: Thursday 27 April 2017 19:09
by Logread
I started writing a plugin that needs to communicate with either remote or localhost Domoticz systems via the regular Json API.

My code to fetch some domoticz json data works perfectly on a standalone basis (using Python 3.6.1)... Here is just a piece of it that illustrates my issue:

Code: Select all

def dzAPICall(APIRequest):
    conn = http.client.HTTPConnection(dzIP, dzPort)
    conn.request("GET", APIRequest)
    response = conn.getresponse()
    if response.status == 200:
        print("Domoticz API Call '" + APIRequest + "' success")
        return json.loads(response.read())
    else:
        print("Domoticz API Call '" + APIRequest + "' failed")
        return False
calling the above function with say listHW = dzAPICall("/json.htm?type=hardware") returns a proper list of hardware in json format

When I include the same code in a Python plugin within Domoticz's framework (even using today's lastest version 3.7312), adjusted to work within a plugin object:

Code: Select all

    def dzAPICall(self, APIRequest):
        conn = http.client.HTTPConnection(Parameters["Address"], Parameters["Port"])
        conn.request("GET", APIRequest)
        response = conn.getresponse()
        if response.status == 200:
            Domoticz.Debug("Domoticz API Call '" + APIRequest + "' success")
            self.onMessage(response.read(), "200", "")
        else:
            Domoticz.Debug("Domoticz API Call '" + APIRequest + "' failed")
I get a 200 return code, but the response.read() does not yield anything that can be decoded with the below:

Code: Select all

    def onMessage(self, Data, Status, Extra):
        Domoticz.Log("onMessage called")
        try:
            strData = Data.decode("utf-8", "ignore")
            Response = json.loads(strData)
        except:
            Domoticz.Error("Invalid data received!")
            return
I have been banging my head on the wall for hours with this... Any idea from the community would be most appreciated
Thanks!

Re: Python plugin: Help with http connection please

Posted: Thursday 27 April 2017 23:50
by Dnpwwo
@Logread,

You don't need to 'adjust' you code to run in the plugin system. The onMessage call back is for when you use the built in framework network connectivity and Domoticz tells you some data has arrived. If you use urllib or http.client for connectivity you don't even need to have an onMessage function.

Did you have a look at the example for doing HTTP in a plugin? https://github.com/domoticz/domoticz/bl ... es/HTTP.py

Re: Python plugin: Help with http connection please

Posted: Sunday 30 April 2017 19:41
by Logread
@Dnpwwo,

Thanks for your response. Actually I did find that just using urllib.request rather then http.client solved my problem ! the Domoticz API finally returns a response to to my python plugin.
I do not understand why http.client was working from Python on a standalone basis but not within the Domoticz framework though... But no big deal now.

Re: Python plugin: Help with http connection please

Posted: Sunday 30 April 2017 21:49
by Westcott
Hi Logread,
If you are using Python3, then simply -
import requests

It's very straightforward.
http://docs.python-requests.org/en/mast ... uickstart/

Re: Python plugin: Help with http connection please

Posted: Sunday 30 April 2017 23:53
by G3rard
I have experienced issues when using the requests module with the Python plugin. The plugin didn't work and no errors are shown related to it. I changed to urllib module.
Check this https://www.domoticz.com/forum/viewtopi ... 10#p126606.

Re: Python plugin: Help with http connection please

Posted: Monday 01 May 2017 5:08
by Dnpwwo
@G3rard,

Your link appears to be to a message about problems importing the 'requests' module.

I don't know anything about that library except that it is nothing to do with Domoticz at all. From a quick look at it seems to do internal threading which is probably why it doesn't work with the single threaded framework.

The internal connectivity and message handling is provided as a part of the Domoticz import itself. I suggested it because it provides native handling for HTTP over TCP/IP which seemed relevant here. Urllib based plugins will always need to rely on device polling while the built in functionality handles asynchronous callbacks which makes the plugin more responsive if the external hardware ever sends that type of message.

Python plugin: Help with http connection please

Posted: Monday 01 May 2017 7:57
by G3rard
@Dnpwwo, thanks for your explanation!
I made my remark regarding requests module based on the post of Westcott above where he suggests to use that module.