Python plugin: Help with http connection please
Posted: Thursday 27 April 2017 19:09
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:
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:
I get a 200 return code, but the response.read() does not yield anything that can be decoded with the below:
I have been banging my head on the wall for hours with this... Any idea from the community would be most appreciated
Thanks!
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
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")
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
Thanks!