本文介绍了如何用请求登录moodle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正尝试使用Python脚本模拟登录,但有";会话过期";错误。以下是代码:
import requests
import re
login = 12345678
passwd = 'password'
r = requests.get("https://moodle.site.com/login/index.php")
cookie = r.cookies.get_dict()
pattern = '<input type="hidden" name="logintoken" value="w{32}">'
token = re.findall(pattern, r.text)
token = re.findall("w{32}", token[0])
payload = {'username': login, 'password': pass, 'anchor': '', 'logintoken': token[0]}
r = requests.post("https://moodle.site.com/login/index.php", cookies=cookie, data=payload)
print(r)
推荐答案
import requests
import re
app_data = {
"login": "LOG",
"password": "PASS",
"url": "https://UR_DOMAIN/login/index.php"
}
def auth_moodle(data: dict) -> requests.Session():
login, password, url_domain = data.values()
s = requests.Session()
r_1 = s.get(url=url_domain + "/login/index.php")
pattern_auth = '<input type="hidden" name="logintoken" value="w{32}">'
token = re.findall(pattern_auth, r_1.text)
token = re.findall("w{32}", token[0])[0]
payload = {'anchor': '', 'logintoken': token, 'username': login, 'password': password, 'rememberusername': 1}
r_2 = s.post(url=url_domain + "/login/index.php", data=payload)
for i in r_2.text.splitlines():
if "<title>" in i:
print(i[15:-8:])
break
counter = 0
for i in r_2.text.splitlines():
if "loginerrors" in i or (0 < counter <= 3):
counter += 1
print(i)
return s
print(auth_moodle(data=app_data))
这篇关于如何用请求登录moodle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!