back
2012/05/25 13:16
Just a snippet : a simple python wrapper around JSON/Freebox V6 API
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cookielib, urllib, urllib2
import json
from pprint import pprint
# install an urlopener which handle cookies
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)
class FBXComm(object):
def __init__(self,host,passwd):
self.__host=host
data={"login":"freebox","passwd":passwd}
request = urllib2.Request("http://%s/login.php"%host, urllib.urlencode(data), {})
r=urllib2.urlopen(request)
assert r.code == 200
fbxsid=None
for i in cookiejar:
if i.name == "FBXSID":
fbxsid=i.value.strip('"')
assert fbxsid
def call(self,method,data):
cgi=method.split(".",1)[0]+".cgi"
data={"jsonrpc":"2.0","method":method,"params":data}
q=urllib2.Request("http://%s/%s"%(self.__host,cgi),json.dumps(data),{"Content-Type":"application/json"})
r=urllib2.urlopen(q)
return json.loads(r.read())
if __name__=="__main__":
x=FBXComm("ton_ip_freebox","ton_mot_de_passe")
pprint( x.call("conn.status",[]) )
pprint( x.call("fs.list",["/Disque dur"]) )
Inspired from chosesafaire
More documentation on Freebox V6 apis
Tags: freebox
|