├── README.md └── onHook.py /README.md: -------------------------------------------------------------------------------- 1 | ## OnHook 2 | 3 | 河畔挂机脚本,断线自动重连 4 | -------------------------------------------------- 5 | 4/8更新 6 | 之前版本出现了session过期后访问报错的情况 7 | 已更新新版本 经验证能正确重连 8 | ![image](https://user-images.githubusercontent.com/52741194/233853456-d2ec112e-581b-408c-80c4-f410c5f93fe5.png) 9 | ## 环境 10 | 11 | - python3 12 | 13 | - lxml 14 | - requests 15 | - urllib3 16 | 17 | ## 运行 18 | ### linux服务器端 19 | 1.在onhook.py data中填写账号密码 20 | 填用户名 使用邮箱或者uid登录需要自行更改loginfield 21 | ``` 22 | data = {'username':'', 23 | 'password':'', 24 | 'loginfield':'username', 25 | 'refer':'https://bbs.uestc.edu.cn/'} 26 | ``` 27 | 28 | 2.上传至云服务器 29 | 30 | ``` 31 | nohup python3 -u onHook.py 32 | ``` 33 | 34 | 退出ssh终端,程序后台运行即可 输出信息默认保存到nohup.out文件中 35 | 运行成功后重新进入终端在同目录下nohup.out下查看是否成功登录以及运行状态 36 | ``` 37 | cat nohup.out 38 | ``` 39 | ![image](https://user-images.githubusercontent.com/52741194/233853565-e5ac676a-0be6-4b5f-8de0-fd0fbf278410.png) 40 | ### windows端 41 | 输入账号密码后直接运行即可 42 | -------------------------------------------------------------------------------- /onHook.py: -------------------------------------------------------------------------------- 1 | # coding=UTF-8 2 | import requests 3 | import urllib3 4 | import time 5 | import re 6 | from lxml import etree 7 | 8 | # 不打印warning 9 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 10 | # 登录时需要POST的数据 11 | data = {'username':'', 12 | 'password':'', 13 | 'loginfield':'username', 14 | 'refer':'https://bbs.uestc.edu.cn/'} 15 | # 设置请求头 16 | headers = { 17 | 'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'} 18 | # 登录时表单提交到的地址 19 | login_url = 'https://bbs.uestc.edu.cn/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LcBaK&inajax=1' 20 | url = 'http://bbs.uestc.edu.cn/forum.php?mod=forumdisplay&fid=174' 21 | 22 | 23 | def login(): 24 | try: 25 | session = requests.Session() 26 | resp = session.post(login_url, data, verify=False,headers=headers) 27 | except BaseException as e: 28 | print(e) 29 | session.close() 30 | print("login exception " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 31 | return session,False 32 | else: 33 | print(resp.content.decode('utf-8')) 34 | pattern = r"欢迎您回来" 35 | m = re.search(pattern, resp.content.decode('utf-8')) 36 | if m.start() > 0: 37 | print("login_success at " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 38 | return session,True 39 | 40 | success=False 41 | while not success: 42 | session, success = login() 43 | 44 | while True: 45 | try: 46 | resp = session.get(url) 47 | except BaseException as e: 48 | success = False 49 | print(e) 50 | session.close() 51 | print("Connection disconnected at " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 52 | print("Reconnecting...") 53 | while not success: 54 | time.sleep(300) 55 | session, success = login() 56 | 57 | else: 58 | 59 | html = etree.HTML(resp.content) 60 | messagetext = html.xpath('//div[@id="messagetext"]/p/text()') 61 | if messagetext and messagetext[0][0:2] == "抱歉": 62 | print("Connection disconnected at " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 63 | print("Reconnecting...") 64 | session = login() 65 | time.sleep(240) # 防止出现验证码 66 | else: 67 | print("login continue at " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 68 | time.sleep(45) 69 | --------------------------------------------------------------------------------