-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.py
44 lines (34 loc) · 1.08 KB
/
auth.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
import yaml
import requests
import hashlib
login_url = "http://net.tsinghua.edu.cn/do_login.php"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1)" \
" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36"
def load_config(path="config/account.yaml"):
with open(path) as f:
config = yaml.load(f)
username = config["account"]["username"]
password = config["account"]["password"]
return username, password
def hex_md5_password(password):
password = hashlib.md5(password).hexdigest()
return "{MD5_HEX}%s" % password
def try_connections():
try:
res = requests.get("http://www.baidu.com")
except requests.HTTPError:
return False
return res.status_code == 200
def go_online():
username, password = load_config()
param = {
"action": "login",
"username": username,
"password": hex_md5_password(password),
"ac_id": 1
}
res = requests.post(login_url, param)
if res.status_code != 200:
return False
else:
return True