Page 1 of 1

How I can do local request without Domoticz connection in a plugin

Posted: Friday 02 November 2018 20:32
by Thorgal789
Hello, I m trying to do a simple local request in http to get JSON data.
The request is http://127.0.0.1:8010/api/3E62E09612/lights/

It don't work with domotics internals commands, I can have JSON from other server (on other url, other port) with the same code but impossible for this one.
The code I m using

Code: Select all

        self.Request = Domoticz.Connection(Name="deCONZ_Com", Transport="TCP/IP", Protocol="HTTP" , Address=Parameters["Address"] , Port=Parameters["Port"])
        self.Request.Connect()
        
        ......
        ......
        
            sendData = { 'Verb' : 'GET',
                         'URL'  : '/api/3E62E09612/lights/',
                         'Headers' : { 'Host': Parameters["Address"] + ':' + Parameters["Port"], \
                                       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', \
                                       'User-Agent':'Domoticz' }
                       }
                       
            self.Request.Send(sendData)
If I enable debug, I can see

Code: Select all

2018-11-02 21:31:13.825 (deCONZ) Received 831 bytes of data
2018-11-02 21:31:13.825 (deCONZ) 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d 0a 41 63 63 HTTP/1.1.200.OK..Acc
2018-11-02 21:31:13.825 (deCONZ) 65 73 73 2d 43 6f 6e 74 72 6f 6c 2d 41 6c 6c 6f 77 2d 4f 72 ess-Control-Allow-Or
2018-11-02 21:31:13.825 (deCONZ) 69 67 69 6e 3a 20 2a 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 igin:.*..Content-Typ
2018-11-02 21:31:13.825 (deCONZ) 65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 3b e:.application/json; 
...
...
So I know It work, but nothing in onMessage() or other command.

It don't work for this Server, but this simple code work without problem out of domoticz

Code: Select all

import urllib.request
fp = urllib.request.urlopen("http://127.0.0.1:8010/api/3E62E09612/lights/")
mybytes = fp.read()
mystr = mybytes.decode("utf8")
fp.close()
print(mystr)

My machine ATM is
Win 7
Domoticz 4.9700
Python 3.6.4

I have lot of problems.
1 - If I take more recent python version, domoticz don't detect them
2 - If I try to use urllib in domoticz > Crash, it work on external link, but not on local, I know Domoticz have lot of problems with urllib, but I haven't see solution yet.

Do I need to use special python version, domoticz version, or if someone know a workaround ?

Re: How I can do local request without Domoticz connection in a plugin

Posted: Friday 02 November 2018 23:31
by Dnpwwo
@Thorgal789,

You can use a later version of Python (test with 3.7 on Windows) if you upgrade move to the Beta version of Domoticz but Python doesn't seem to be the issue.

Best guess is a malformed HTTP response from the webserver. The plugin framework calls onMessage for *complete* messages for a given protocol so it is probably waiting for more data. Compare the response length header with the actual response length to see if that is the case.

Re: How I can do local request without Domoticz connection in a plugin

Posted: Saturday 03 November 2018 13:49
by Thorgal789
Yep, I think the server use a no standard response, but it works without problem in firefox/IE/python/ect ... So for me the prb not come from it.
And you are right, the framework is freezed, if I force a disconnect the callback onMessage work just after.

BTW, I have found a working way, it's the protocol http that is buggy (it bug too with the websocket), I m using protocol=None and use raw command

Code: Select all

        self.Request = Domoticz.Connection(Name="deCONZ_Com", Transport="TCP/IP", Address=Parameters["Address"] , Port=Parameters["Port"])
        self.Request.Connect() 
...
...
            sendData = "GET " + url + " HTTP/1.1\r\n" \
                        "Host: " + Parameters["Address"] + ':' + Parameters["Port"] + "\r\n" \
                        "User-Agent: Domoticz/1.0\r\n" \
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \
                        "Connection: keep-alive, Upgrade\r\n\r\n"
           self.Request.Send(sendData)
But it's a rly heavy method just to make a request on local network. Asynchronous transport are useless in this case, and out of domoticz, it's possible with one command line in all languages (LUA/python/shell) but IDK how to make it with a proper method inside a python plugin.