Sending JSON command from Python
#1
This might be helpful to someone but here is a small function to send a JSON command to XBMC and parse the response ::

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.
Reply


Messages In This Thread
Sending JSON command from Python - by sffjunkie - 2011-01-26, 15:54
[No subject] - by topfs2 - 2011-01-26, 16:34
[No subject] - by daledude - 2011-01-26, 17:32
[No subject] - by topfs2 - 2011-01-26, 17:34
[No subject] - by daledude - 2011-01-26, 17:43
[No subject] - by sffjunkie - 2011-01-27, 11:09
Logout Mark Read Team Forum Stats Members Help
Sending JSON command from Python0