my first python, need some help

Posted on
Tue Jun 23, 2015 9:56 pm
jlTech offline
Posts: 21
Joined: Jun 20, 2015

my first python, need some help

hello
need some help with my first python script. I suspect i have done it wrong but i cannot find the error....
Trying to upload current temp to a website (temperatur.nu)

Code: Select all
import httplib, urllib

curTemp = indigo.devices[1204901047].displayStateValUi
id = "6488954"
city = "hofors"
params = urllib.urlencode({'s': city, 'id': id, 't': curTemp})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

conn = httplib.HTTPConnection("www.temperatur.nu:80")
conn.request("POST", "/rapportera.php?", params, headers)
response = conn.getresponse()
print response.status, response.reason

data = response.read()
indigo.server.log(data)
conn.close()

# just some logging to see what i got
indigo.server.log(str(curTemp))
indigo.server.log(str(conn))
indigo.server.log(str(params))


here is the response i get. I suspect it is the connection string that is wrong some how. Translate first row (Fatal, failure! Could not match city and id) :)
Code: Select all
 
Script                          Fatal, failure! Kan inte inte matcha stad och id!
  Script                          10.5
  Script                          <httplib.HTTPConnection instance at 0x5e3dc88>
  Script                          s=hofors&id=6488954&t=10.5

Posted on
Tue Jun 23, 2015 11:31 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: my first python, need some help

My Swedish is non-existent, but two points to note:

1) That's the same error you get if you go to rapportera.php in a browser without any values

2) The example URL in the wiki shows two parameters called hash and t, and it says you will get given a hash key when you register your interest

If I was diagnosing this from scratch I'd be wondering if you need to remove the ? from the conn.request line?


Sent from my iPhone using Tapatalk

Posted on
Tue Jun 23, 2015 11:56 pm
jlTech offline
Posts: 21
Joined: Jun 20, 2015

Re: my first python, need some help

This URL is working in my fibaro using lua.
Did try without ? but same error


Regards Jonny Larsson
From my iPhone6+ med Tapatalk

Posted on
Wed Jun 24, 2015 12:57 am
jlTech offline
Posts: 21
Joined: Jun 20, 2015

Re: my first python, need some help

This work so i think it is something with the params row

Code: Select all
import httplib, urllib

curTemp = indigo.devices[1204901047].displayStateValUi
id = "6488954"
city = "hofors"
params = urllib.urlencode({'s': city, 'id': id, 't': curTemp})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

conn = httplib.HTTPConnection("www.temperatur.nu:80")
conn.request("POST", "/rapportera.php?s=" + city + "&id=" + id + "&t=" + curTemp, params, headers)
response = conn.getresponse()
print response.status, response.reason

data = response.read()
indigo.server.log("Temperatur updated " + data)
conn.close()


Posted on
Wed Jun 24, 2015 5:18 am
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: my first python, need some help

conn.request("POST", "/rapportera.php?s=" + city + "&id=" + id + "&t=" + curTemp, params, headers)

Not sure if this is your issue, but you seem to be sending the data twice -- once on the URL query string and again in the body of the request. If you continue to include it in the query string you really should URL Encode it, though as long as you know the exact city/ID that will be passed it then it may not be an issue.

Adam

Posted on
Wed Jun 24, 2015 5:23 am
jlTech offline
Posts: 21
Joined: Jun 20, 2015

Re: my first python, need some help

When i did change from
Code: Select all
conn.request("POST", "/rapportera.php?", params, headers)


to this it works.
Code: Select all
conn.request("POST", "/rapportera.php?s=" + city + "&id=" + id + "&t=" + curTemp, params, headers)


Perhaps not a good script, its my first so.... :?

Posted on
Wed Jun 24, 2015 8:34 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: my first python, need some help

Your URL line (with params) looks like a GET, not a POST. Are you sure it's a POST? Also, in your original script, you don't need to urlencode the params since it's going as POST data. If it does accept a POST, then that's what's wrong there. And the url should just end with the .php (no ? or params).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests

cron