2011-01-26, 15:54
This might be helpful to someone but here is a small function to send a JSON command to XBMC and parse the response ::
just change the xbmc_host and xbmc_port values or pass them as parameters.
Code:
import json
import httplib
import base64
def send_json_command(method, params=None, id=1, username='', password=''):
command = {'jsonrpc': '2.0', 'method': method, 'id': id}
if params is not None:
command['params'] = params
payload = json.dumps(command, ensure_ascii=False)
payload.encode('utf-8')
headers = {'Content-Type': 'application/json-rpc; charset=utf-8'}
if self.password != '':
userpass = base64.encodestring('%s:%s' % (username, password))[:-1]
headers['Authorization'] = 'Basic %s' % userpass
conn = httplib.HTTPConnection(xbmc_host, xbmc_port)
conn.request('POST', '/jsonrpc', payload, headers)
response = conn.getresponse()
data = None
if response.status == 200:
data = response.read()
conn.close()
if data is not None:
return json.loads(data)
else:
return None
just change the xbmc_host and xbmc_port values or pass them as parameters.