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