-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
51 lines (40 loc) · 1.2 KB
/
net.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
# coding=utf-8
import socket
import requests
from bs4 import BeautifulSoup
import re
from datetime import datetime
import time
from pygame import mixer
warning_flow = 0
def getflow(n):
while True:
global warning_flow
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
localip = s.getsockname()[0]
url = 'http://netflow.dorm.ccu.edu.tw/flows/' + str(localip)
# url = 'http://netflow.dorm.ccu.edu.tw/flows/'+ '140.123.222.100'
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
flow = soup.find('h3')
dr = re.compile(r'<[^>]+>', re.S)
dd = dr.sub('', str(flow))
MB = re.sub("[^0-9 ^.]", "", dd)
# print(dd)
# print(float(MB))
if float(MB) > warning_flow:
print('流量警告 現有流量' + MB + 'MB')
mixer.init()
mixer.music.load('warning.mp3')
mixer.music.play()
else:
print('現有流量' + MB + 'MB')
s.close()
time.sleep(n)
try:
print('請輸入流量限制:')
warning_flow = int(input())
getflow(30)
except:
print("請輸入有效數字")