Problem with the python2 rest streaming sample code


#1

Granted, I’m definitely no Python wiz, but am I not supposed to get some output from the REST streaming code example? The Curl version works just fine but I can’t seem to get the Python 2 example to output anything. And yes, I have included the correct token :slight_smile: Any suggestions greatly appreciated.
Talking about this one: https://developers.nest.com/guides/api/rest-streaming-guide#subscribe_to_changes_in_structures_and_devices


#2

When you say “to output anything” what do you mean exactly… while it’s running, if you change something on whatever device you authorized it doesn’t output “the data has changed” or anything like that?


#3

sorry for being vague. if i run the curl statement i get the expected event : put data{… when something happens and the event: keep-alive when nothing is going on. if i run the python example it just dumps me back to the command line. i had expected it to “listen” like the curl example. but perhaps i’m mistaken?


#4

Hi jonas,
For this sample, you would need to install sseclient-py (not sseclient). However it still is imported as ‘sseclient’ in the code. You probably have https://pypi.org/project/sseclient/ installed, which is similar enough to not have any error messages within this sample, but unfortunately doesn’t work with it :frowning:
To use the Python 2 (or 3) code example, you will need to install https://pypi.python.org/pypi/sseclient-py/1.7.

pip install sseclient-py

#5

thank you very much for your reply becky however, unfortunately it was’t quite that easy as i already have sseclient-py installed.


#6

Thanks jonas. We will check if something has changed with the libraries or environment and add a note or update the sample.


#7

Hi jonas,
There was a default setting that changed so we will be updating the samples soon. If you prefer you can try this change:

replace this line of code in the sample:
response = http.request(‘GET’, url, headers=headers, preload_content=False)

with the following code:
response = http.request(‘GET’, url, headers=headers, preload_content=False, redirect=False)
if (response.status == 307):
redirect_url = response.headers.get(“Location”)
response = http.request(‘GET’, redirect_url, headers=headers,
preload_content=False, redirect=False)
if (response.status != 200):
print "An error occurred! Response code is ", response.status

Thanks for hanging in there on this and finding this issue!


#8

works perfectly. thank you!