-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathled_on.py
55 lines (45 loc) · 1.35 KB
/
led_on.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3
import xml.etree.ElementTree as ET
from urllib import request, parse
import hashlib
# configuration
FRITZBOX_PASSWORD = ''
FRITZBOX_USER = ''
FRITZBOX_BASE_URL = ''
# method to retrieve SID
def get_sid():
fb = request.urlopen(FRITZBOX_BASE_URL + 'login_sid.lua')
dom = ET.parse(fb)
sid = dom.findtext('./SID')
challenge = dom.findtext('Challenge')
if sid == '0000000000000000':
md5 = hashlib.md5()
md5.update(challenge.encode('utf-16le'))
md5.update('-'.encode('utf-16le'))
md5.update(FRITZBOX_PASSWORD.encode('utf-16le'))
response = challenge + '-' + md5.hexdigest()
if FRITZBOX_USER:
url = FRITZBOX_BASE_URL + 'login_sid.lua?username=' + FRITZBOX_USER + '&response=' + response
else:
url = FRITZBOX_BASE_URL + 'login_sid.lua?&response=' + response
fb = request.urlopen(url)
dom = ET.parse(fb)
sid = dom.findtext('./SID')
if sid == '0000000000000000':
raise PermissionError('access denied')
return sid
# get a valid SID
sid = get_sid()
# HTTP Post request body content
datadict = {'xhr': 1,
'led_display': 0,
'apply': '',
'sid': sid,
'lang': 'de',
'page': 'led'}
data = parse.urlencode(datadict).encode()
req = request.Request(FRITZBOX_BASE_URL + "data.lua", data=data)
# execute request to switch LEDs
resp = request.urlopen(req)
# log out
request.urlopen(FRITZBOX_BASE_URL + "home/home.lua?logout=1&sid=" + sid)