Python Plugin: request from url

Python and python framework

Moderator: leecollings

Post Reply
Noudje
Posts: 16
Joined: Monday 25 January 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta

Python Plugin: request from url

Post by Noudje »

I like to get date from an url like www.domain.com?p1=a&p2=b&p3=b. Is that possible with Domoticz.Connection'? I already tried several options with https://github.com/domoticz/domoticz/bl ... es/HTTP.py as an example, but always got the message:
resolve: Host not found (authoritative)
What is the correct procedure to retrieve a response from an url like 'www.domain.com?p1=a&p2=b&p3=b'?
Raspberry Pi 2 with latest beta version
1x Aeon Labs USB Z-Stick S2
5x Fibaro Modules
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: request from url

Post by Dnpwwo »

@Noudje,

Have a look at https://github.com/domoticz/domoticz/bl ... es/HTTP.py. It does a get on "/" but you could change it to "?p1=a&p2=b&p3=b" at line 47 and set the base domain via the Hardware page.
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
febalci
Posts: 331
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Python Plugin: request from url

Post by febalci »

For the:

Code: Select all

self.httpConn = Domoticz.Connection(Name="HTTP Test", Transport="TCP/IP", Protocol="HTTP", Address=Parameters["Address"], Port=Parameters["Port"])
line, the address parameter should only be the domain name: www.domain.com

For the other lines:

Code: Select all

            sendData = { 'Verb' : 'GET',
                         'URL'  : '/?p1=a&p2=b&p3=b', # or you may have to use '/inde.html?p1=a&p2=b&p3=b'
                         'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
                                       'Connection': 'keep-alive', \
                                       'Accept': 'Content-Type: text/html; charset=UTF-8', \
                                       'Host': Parameters["Address"]+":"+Parameters["Port"], \
                                       'User-Agent':'Domoticz/1.0' }
}
Please note that HTTP usage in stable and latest beta is different. This will not work with the stable.
Noudje
Posts: 16
Joined: Monday 25 January 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta

Re: Python Plugin: request from url

Post by Noudje »

Thnx for the help!

Meanwhile I found another solution. Now I use urllib to retrieve a csv file from the url.
Raspberry Pi 2 with latest beta version
1x Aeon Labs USB Z-Stick S2
5x Fibaro Modules
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: request from url

Post by Dnpwwo »

@Noudje,

No problem. Just be aware that using urllib will stall the plugin framework while your plugin waits for a response because urllib is synchronous. That won't be a problem if you only run 1 or 2 plugins but can be if you run more than that.

The built in connection solution is asynchronous so will scale much better.
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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest