Page 1 of 1
Plugin urllib2 issue (Nest thermostat plugin)
Posted: Wed Mar 19, 2014 1:35 am
by Dragon470
I am relatively uncertain on some areas of python. This being one of them.
I have a problem with my python code for the Nest thermostat plugin. The company's servers have been using sslv3 for their https connections. A few days ago (March 14th 2014) they changed to tlsv1. I currently use:
Code: Select all
response = urllib2.urlopen(request).read()
I have no idea what needs to be changed to force this protocol usage. I thought urllib2 automatically detected and used the correct protocol.
Re: Plugin urllib2 issue (Nest thermostat plugin)
Posted: Wed Mar 19, 2014 6:38 am
by krambriw
Maybe you should consider using 'requests'. It's a python library, I use it and it worked very well where I was earlier unsuccessful with urllib and urllib2.
http://docs.python-requests.org/en/latest/
Check this out. We can then continue the discussion, I have provided you with a simple sample below and you could then just try to logon and capture the pages starting from a small python script (before fixing the plugin).
Basically, what I did, I installed the library with the setup. Then I just copied the subdirectory 'requests' and include that as a subdirectory to my plugin. It might be possible to install this one time in EG somehow but for the time, it will be included in the plugin distro.
Below is the sample python script that you could elaborate with. For convenience I have also attached the 'request' sub that you can start with if you want. To use with python scripts in EG, just unzip it in the main EG folder (to use with a plugin, unzip it in the actual plugin folder as a sub).
Best regards, Walter
Code: Select all
from requests import session
username = 'myself'
password = 'passw'
theUrl = (
'https://mypages.xxx.com/xxx_security_check?locale=xz_YW&j_username='
+username
+'&j_password='
+password
+'&xxx-security-redirect=%2Fse%2Fstart.html'
)
s = session()
#to logon
r = s.post(theUrl)
print r.text
#if you are looking for a specific page section
start = r.text.find('start keyword')
end = r.text.find('end keyword',start)
txt_section = r.text[start:end]
print txt_section
#other good methods
#get a page
#thePage = s.get('https://mypages.xxx.com/news')
#some status infos and other useful stuff
#print thePage.info()
#print thePage.text
#print thePage.headers['content-type']
#print thePage.headers
#print thePage.status_code
#print thePage.encoding
Re: Plugin urllib2 issue (Nest thermostat plugin)
Posted: Wed Mar 19, 2014 4:53 pm
by Dragon470
Thanks Walter,
I looked in to the requests, and tested but I still get the exact same response as with urllib2:
SSLError: [Errno 8] _ssl.c:480: EOF occurred in violation of protocol
so my test script was:
Code: Select all
import urllib
import urllib2
import requests
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
print "No json library available. I recommend installing either python-json"
print "or simplejson."
sys.exit(-1)
def loads(res):
if hasattr(json, "loads"):
res = json.loads(res)
else:
res = json.read(res)
return res
data = urllib.urlencode({"username": "***", "password": "***"})
req = urllib2.Request("https://home.nest.com/user/login",
data,
{"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4"})
res = urllib2.urlopen(req).read()
res = loads(res)
transport_url = res["urls"]["transport_url"]
access_token = res["access_token"]
userid = res["userid"]
print transport_url
print access_token
print userid
res = requests.get(transport_url + "/v2/mobile/user." + userid,
headers={"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4",
"Authorization":"Basic " + access_token,
"X-nl-user-id": userid,
"X-nl-protocol-version": "1"})
print res["structure"].keys()[0] #This just prints one of the data points
It works right up to res = request.get()
The first section is to login.
I know you can't run it, but I don't know what I am doing wrong, or need to change.
Re: Plugin urllib2 issue (Nest thermostat plugin)
Posted: Wed Mar 19, 2014 7:53 pm
by krambriw
I don't know, but have you tried request.post() as well?
Re: Plugin urllib2 issue (Nest thermostat plugin)
Posted: Sat Mar 22, 2014 1:17 pm
by Dragon470
I really wish to thank you Walter. I have found that it takes a little more than what I want to do initially, but it can be done with the requests library.
Re: Plugin urllib2 issue (Nest thermostat plugin)
Posted: Sat Mar 22, 2014 2:03 pm
by krambriw
Oh great, happy to hear
Yes, the requests library is a nice thing, especially since they claim that the urllib2 is broken, I do not know if it is true or not. Anyway, requests takes away the hazzle, especially with https connections.
Also consider to include the license information in the header of your plugin:
Code: Select all
##############################################################################
# Acknowledgement
#
# This plugin is using Requests, the excellent Apache2 Licensed HTTP library,
# written in Python, for human beings, http://www.python-requests.org/en/latest/
#
# Copyright 2013 Kenneth Reitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
BestR, Walter