Last active 1505425999

A script for getting connection statistics from a Vivid Wireless-supplied Huawei B315s-607. Other models may have a similar feature.

Revision 332e4c85ef09142231ffddc221b2a5e37fc6deaf

output.txt Raw
1$ python3 usage.py
2CurrentConnectTime: 12:47:44
3CurrentUpload: 817.37MB
4CurrentDownload: 27.28GB
5CurrentDownloadRate: 3.50MB/s
6CurrentUploadRate: 409.60KB/s
7TotalUpload: 821.38MB
8TotalDownload: 27.31GB
9TotalConnectTime: 13:21:02
vivid-huawei.py Raw
1#!/usr/bin/env python3
2import requests
3from xml.etree import ElementTree
4
5ROUTER_IP = "192.168.17.1"
6
7def b2B(num_of_bytes):
8 if type(num_of_bytes) not in (float, int):
9 num_of_bytes = float(num_of_bytes)
10 opts, i = "KMGTP", -1
11 while num_of_bytes > 1000:
12 num_of_bytes = num_of_bytes / 1000
13 i += 1
14 return "{:.2f}{}B".format(num_of_bytes, opts[i])
15
16def hms(s):
17 m,s = divmod(s,60)
18 h,m = divmod(m,60)
19 d,h = divmod(h,24)
20 y,d = divmod(d,365)
21 return y,d,h,m,s
22
23i = requests.get("http://{}/html/home.html".format(ROUTER_IP))
24d = ElementTree.fromstring(requests.get("http://{}/api/monitoring/traffic-statistics".format(ROUTER_IP), headers={"Cookie": i.headers["Set-Cookie"]}).text)
25for e in d:
26 if e.tag in ["CurrentConnectTime", "TotalConnectTime"]:
27 print("{}: {}".format(e.tag, ":".join(str(x).zfill(2) for x in hms(int(e.text)) if x)))
28 if e.tag in ["CurrentUpload", "CurrentDownload", "TotalUpload", "TotalDownload"]:
29 print("{}: {}".format(e.tag, b2B(e.text)))
30 if e.tag in ["CurrentUploadRate", "CurrentDownloadRate"]:
31 print("{}: {}/s".format(e.tag, b2B(float(e.text)*1024)))