Urllib2 http to kodi error

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
holdestmade
Experienced User
Posts: 135
Joined: Thu Dec 04, 2014 2:44 pm

Urllib2 http to kodi error

Post by holdestmade » Tue Oct 24, 2017 11:03 am

Hope someone can help with this.

I use the python code below to send a notification to several Kodi instances and all works great, except when one or more is turned off then I get a traceback error and it stops the whole script.

Is there a way to test if Kodi is reachable before running the code below ?

I have tried the XBMC2 plugin and that doesn't error but on Kodi I get a notification about a new connection before the notification I send and would prefer not to have that.

Thanks

Code: Select all

urllib2.urlopen('http://' + kodi_ip + ':8080/jsonrpc?request={"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"' + title + '","message":"' + message + '","image":"","displaytime":' + str(duration*1000) + '},"id":1}')

eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Urllib2 http to kodi error

Post by eirik226 » Tue Oct 24, 2017 11:22 am

I don't really know how to code it but if you use error handling in the python code it should solve it.

I don't know how Kodi will respond but for websites it's 200 ok, 404 not found and 400 bad requests.

So you can basically solve it two ways I think, with IF statement, aka if respond ok continue else stop.

or with try statement:

https://docs.python.org/2/tutorial/errors.html

You should look at 8.3 --> https://docs.python.org/2/tutorial/erro ... exceptions

Edit: Something like this, I think should work:

Code: Select all

import urllib2

kodi_ip = '10.0.0.10'
title = 'test'
message = 'test'
duration = 10

try:
    urllib2.urlopen('http://' + kodi_ip + ':8080/jsonrpc?request={"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"' + title + '","message":"' + message + '","image":"","displaytime":' + str(duration*1000) + '},"id":1}')
except:
    print 'ERROR'
You can add more function to it as well if you want, how long the timeout should be etc.

EDIT 2: If you take a look at this URL you should get some ideas: https://stackoverflow.com/questions/249 ... -reachable

holdestmade
Experienced User
Posts: 135
Joined: Thu Dec 04, 2014 2:44 pm

Re: Urllib2 http to kodi error

Post by holdestmade » Tue Oct 24, 2017 4:52 pm

Wow, that works perfect ! many thanks.

I'm reading up the link you sent for next time !

Cheers

Post Reply