Python plugin: Help with http connection please

Python and python framework

Moderator: leecollings

Post Reply
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Python plugin: Help with http connection please

Post 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!
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python plugin: Help with http connection please

Post 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
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Help with http connection please

Post 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.
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Python plugin: Help with http connection please

Post 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/
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Python plugin: Help with http connection please

Post 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.
Not using Domoticz anymore
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python plugin: Help with http connection please

Post 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.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Python plugin: Help with http connection please

Post 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.
Not using Domoticz anymore
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest