Python Connection Question

Python and python framework

Moderator: leecollings

Post Reply
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Python Connection Question

Post by wizzard72 »

Hi,

I'm trying to develop a plugin. I'm not a developer, it's more learning on the job :-) Couple of days I'm banging my head to make a connection to a API website. But first I have to login to get to the goods.

If I do it from the commandline it's working perfect:

Code: Select all

curl --silent --cookie /tmp/json_c-H "Content-Type: application/json" -X POST -d "{\"username\":\"USERNAME\",\"password\":\"Welcome123\"}" https://IPAddress:8443/api/login
So far I have this code:

Code: Select all

    def onStart(self):
        Domoticz.Log("Calling onStart")
        self.httpConn = Domoticz.Connection(Name="Test", Transport="TCP/IP", Protocol="HTTPS", Address="172.0.0.1", Port="8443")
        self.httpConn.Connect()
        Domoticz.Log("Ending onStart")


    def onStop(self):
        Domoticz.Log("Plugin is stopping.")
        Domoticz.Debugging(0)

    def onConnect(self, Connection, Status, Description):
        Domoticz.Log("Calling onConnect")
        Domoticz.Log("Status = "+str(Status))
        if (Status == 0):
            Domoticz.Debug("Trying to login to the api.")
            sendData = { 'Verb' : 'POST',
                         'URL'  : '/api/login',
                         'Headers' : { 'Content-Type': 'application/json; charset=utf-8', \
                                       'Connection': 'keep-alive', \
                                       'Accept': 'Content-Type: application/json; charset=UTF-8', \
                                       'User-Agent':'Domoticz/1.0' },
                         'Data' : { 'username':'USERNAME','password':'Welcome123' }
                       }
            Connection.Send(sendData)
            DumpHTTPResponseToLog(sendData)
            Domoticz.Log("Successfully logged in.")
The problem is that it doesn't login to the API website.
Can someone help me?
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 Connection Question

Post by Dnpwwo »

@wizzard72,

Your curl command is sending a JSON payload {"username":"USERNAME","password":"Welcome123"} as a string.
Your plugin is telling the plugin framework to send the 2 strings in the Data dictionary (keys are: 'username' and 'password') not as JSON at all.

Try something like:

Code: Select all

            postData = '{ "username":"USERNAME","password":"Welcome123" }'
            sendData = { 'Verb' : 'POST',
                         'URL'  : '/api/login',
                         'Headers' : { 'Content-Type': 'application/json; charset=utf-8', \
                                       'Connection': 'keep-alive', \
                                       'Accept': 'Content-Type: application/json; charset=UTF-8', \
                                       'User-Agent':'Domoticz/1.0' },
                         'Data' : postData
                       }
            Connection.Send(sendData)
 
note that the single quotes around the data is the key thing here
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
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Connection Question

Post by wizzard72 »

@Dnpwwo,

Thank you for explaining. But still I cannot get authenticated by the website/api.
I'm going to try some more things.
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 Connection Question

Post by Dnpwwo »

@wizzard72,

I would suggest turning on connection message debugging to see what the framework is actually sending (see https://www.domoticz.com/wiki/Developin ... llable_API)

Something like this in onStart:

Code: Select all

	Domoticz.Debugging(16 | 64)
then post the result
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
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Connection Question

Post by wizzard72 »

@Dnpwwo,

I added the debugging in the onStart section, here is the log:
2019-03-01 09:22:37.924 (Test) Calling onStart
2019-03-01 09:22:37.924 (Test) Debug logging mask set to: CONNECTION MESSAGE
2019-03-01 09:22:37.924 (Test) Protocol set to: 'HTTPS'.
2019-03-01 09:22:37.924 (Test) Transport set to: 'TCP/IP', <SERVERNAME>:8443.
2019-03-01 09:22:37.936 (Test) Connect directive received, action initiated successfully.
2019-03-01 09:22:37.789 Status: (Test) Started.
2019-03-01 09:22:37.923 Status: (Test) Entering work loop.
2019-03-01 09:22:37.923 Status: (Test) Initialized version 0.0.1, author 'Wizzard'
2019-03-01 09:22:38.440 (Test) Calling onConnect
2019-03-01 09:22:38.440 (Test) Status = 0
2019-03-01 09:22:38.440 (Test) 1.LOG:
2019-03-01 09:22:38.440 (Test) TEST Controller: Successfully logged in.
2019-03-01 09:22:38.441 (Test) Debug logging mask set to: CONNECTION MESSAGE
2019-03-01 09:22:38.441 (Test) 2.LOG:
2019-03-01 09:22:38.441 (Test) 2.Good Response received from TEST Controller, Disconnecting.
2019-03-01 09:22:38.441 (Test) Sending 251 bytes of data
2019-03-01 09:22:38.441 (Test) 50 4f 53 54 20 2f 61 70 69 2f 6c 6f 67 69 6e 20 48 54 54 50 POST./api/login.HTTP
2019-03-01 09:22:38.441 (Test) 2f 31 2e 31 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 44 6f /1.1..User-Agent:.Do
2019-03-01 09:22:38.441 (Test) 6d 6f 74 69 63 7a 2f 31 2e 30 0d 0a 43 6f 6e 6e 65 63 74 69 moticz/1.0..Connecti
2019-03-01 09:22:38.442 (Test) 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69 76 65 0d 0a 43 6f 6e 74 on:.keep-alive..Cont
2019-03-01 09:22:38.442 (Test) 65 6e 74 2d 54 79 70 65 3a 20 61 70 70 6c 69 63 61 74 69 6f ent-Type:.applicatio
2019-03-01 09:22:38.442 (Test) 6e 2f 6a 73 6f 6e 3b 20 63 68 61 72 73 65 74 3d 75 74 66 2d n/json;.charset=utf-
2019-03-01 09:22:38.442 (Test) 38 0d 0a 41 63 63 65 70 74 3a 20 43 6f 6e 74 65 6e 74 2d 54 8..Accept:.Content-T
2019-03-01 09:22:38.442 (Test) 79 70 65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f ype:.application/jso
2019-03-01 09:22:38.442 (Test) 6e 3b 20 63 68 61 72 73 65 74 3d 55 54 46 2d 38 0d 0a 43 6f n;.charset=UTF-8..Co
2019-03-01 09:22:38.442 (Test) 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 35 31 0d 0a 0d 0a ntent-Length:.51....
2019-03-01 09:22:38.443 (Test) 20 7b 20 22 75 73 65 72 6e 61 6d 65 22 3a 22 55 53 45 52 4e .{."username":"USERN
2019-03-01 09:22:38.443 (Test) 41 4d 45 22 2c 22 70 61 73 73 77 6f 72 64 22 3a 22 57 65 6c AME","password":"Wel
2019-03-01 09:22:38.443 (Test) 63 6f 6d 65 31 32 33 22 20 7d 20 .. .. .. .. .. .. .. .. .. come123".}.
2019-03-01 09:22:38.443 (Test) Sending 176 bytes of data
2019-03-01 09:22:38.443 (Test) 47 45 54 20 2f 61 70 69 2f 73 2f 64 65 66 61 75 6c 74 2f 73 GET./api/s/default/s
2019-03-01 09:22:38.444 (Test) 74 61 74 2f 73 74 61 20 48 54 54 50 2f 31 2e 31 0d 0a 55 73 tat/sta.HTTP/1.1..Us
2019-03-01 09:22:38.444 (Test) 65 72 2d 41 67 65 6e 74 3a 20 44 6f 6d 6f 74 69 63 7a 2f 31 er-Agent:.Domoticz/1
2019-03-01 09:22:38.444 (Test) 2e 30 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 70 .0..Connection:.keep
2019-03-01 09:22:38.444 (Test) 2d 61 6c 69 76 65 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 -alive..Content-Type
2019-03-01 09:22:38.444 (Test) 3a 20 74 65 78 74 2f 78 6d 6c 3b 20 63 68 61 72 73 65 74 3d :.text/xml;.charset=
2019-03-01 09:22:38.444 (Test) 75 74 66 2d 38 0d 0a 41 63 63 65 70 74 3a 20 43 6f 6e 74 65 utf-8..Accept:.Conte
2019-03-01 09:22:38.444 (Test) 6e 74 2d 54 79 70 65 3a 20 74 65 78 74 2f 78 6d 6c 3b 20 63 nt-Type:.text/xml;.c
2019-03-01 09:22:38.445 (Test) 68 61 72 73 65 74 3d 55 54 46 2d 38 0d 0a 0d 0a .. .. .. .. harset=UTF-8....
2019-03-01 09:22:38.451 (Test) Queued asyncronous secure read aborted.
2019-03-01 09:22:38.496 (Test) Received 106 bytes of data
2019-03-01 09:22:38.496 (Test) 48 54 54 50 2f 31 2e 31 20 34 30 30 20 0d 0a 54 72 61 6e 73 HTTP/1.1.400...Trans
2019-03-01 09:22:38.496 (Test) 66 65 72 2d 45 6e 63 6f 64 69 6e 67 3a 20 63 68 75 6e 6b 65 fer-Encoding:.chunke
2019-03-01 09:22:38.496 (Test) 64 0d 0a 44 61 74 65 3a 20 46 72 69 2c 20 30 31 20 4d 61 72 d..Date:.Fri,.01.Mar
2019-03-01 09:22:38.496 (Test) 20 32 30 31 39 20 30 38 3a 32 32 3a 33 38 20 47 4d 54 0d 0a .2019.08:22:38.GMT..
2019-03-01 09:22:38.496 (Test) 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 0d 0a 0d Connection:.close...
2019-03-01 09:22:38.497 (Test) 0a 30 0d 0a 0d 0a .. .. .. .. .. .. .. .. .. .. .. .. .. .. .0....
2019-03-01 09:22:38.497 (Test) Disconnect event received for '<SERVERNAME>:8443'.
2019-03-01 09:22:38.497 (Test) Calling onMessage
2019-03-01 09:22:38.497 (Test) onDisconnect
2019-03-01 09:22:38.497 (Test) onDisconnect called for connection to: <SERVERNAME>:8443
2019-03-01 09:22:38.440 Status: (Test) Launching websocket
2019-03-01 09:22:38.497 Error: (Test) TEST returned a Bad Request Error.
2019-03-01 09:22:47.981 (Test) onHeartbeat
There is a Bad Request Error (400). Any idea?
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 Connection Question

Post by Dnpwwo »

@wizzard72,

I suggest you strip your plugin back to the minimum.
  • You shouldn't send the GET statement until you get a successful response from your POST because HTTP is normally a 1 in 1 out protocol.
  • Remove extra HTTP headers, the curl command only specifies one (Content-Type: application/json)
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