如何用请求登录moodle?

How to login moodle with requests?(如何用请求登录moodle?)
本文介绍了如何用请求登录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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)