如何用请求登录moodle?

发布时间:2022-06-18 / 作者:清心寡欲
本文介绍了如何用请求登录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 = ''
 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 = ''
    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 "" 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))</code></pre>




                        <p>这篇关于如何用请求登录moodle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!</p>
<br><br><p>[英文标题]How to login moodle with requests?</p>
                    <br>
                    <p>
                        声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。
                    </p>
                </div>
                <!-- ad article -->
                <script>pra('3', "article");</script>
            </div>
            <div class="recommend-panel">
                <!-- ad article_line -->
                <script>pr('3', "article_line");</script>
            </div>
            <div class="recommend-panel">
                <h4 class="right-card-title">相关文章</h4>
                <div class="card-deck">
                                        <div class="card ll-panel">
                        <a class="recommend-panel-link" href="https://www.ijwell.com/a/Ae4wz2.html" title="将变量传递给CURLOPT_POSTFIELDS c++">
                            <div class="recommend-panel-top">
                                <img src="https://static.ijwell.com/public/static/images/cover999/635.jpg" class="img-fluid" alt="将变量传递给CURLOPT_POSTFIELDS c++">
                            </div>
                            <div class="recommend-panel-bottom">
                                将变量传递给CURLOPT_POSTFIELDS c++                            </div>
                        </a>
                    </div>
                                        <div class="card ll-panel">
                        <a class="recommend-panel-link" href="https://www.ijwell.com/a/Id8CbG.html" title="在使用CultureInfo时,如何在数字前面显示欧元符号?">
                            <div class="recommend-panel-top">
                                <img src="https://static.ijwell.com/public/static/images/cover999/750.jpg" class="img-fluid" alt="在使用CultureInfo时,如何在数字前面显示欧元符号?">
                            </div>
                            <div class="recommend-panel-bottom">
                                在使用CultureInfo时,如何在数字前面显示欧元符号?                            </div>
                        </a>
                    </div>
                                        <div class="card ll-panel">
                        <a class="recommend-panel-link" href="https://www.ijwell.com/a/I9ys8U.html" title="将值从要素文件传递到Speflow方案方法">
                            <div class="recommend-panel-top">
                                <img src="https://static.ijwell.com/public/static/images/cover999/270.jpg" class="img-fluid" alt="将值从要素文件传递到Speflow方案方法">
                            </div>
                            <div class="recommend-panel-bottom">
                                将值从要素文件传递到Speflow方案方法                            </div>
                        </a>
                    </div>
                                    </div>
            </div>
        </div>

        <div class="col-md-3 cl-right">
            <div class="right-card">
                <div class="right-card-main">
                    <div class="right-card-title">热门文章</div>
                    <ul class="right-recommended-list">
                                                <li><a href="https://www.ijwell.com/a/e4GxCg.html" title="图形DrawPath在呈现文本时产生意外结果"><i class="fa fa-angle-right" aria-hidden="true"></i>图形DrawPath在呈现文本时产生意外结果</a></li>
                                                <li><a href="https://www.ijwell.com/a/6QoCxT.html" title="LabVIEW互操作组件,在C#和LabVIEW之间交换图像"><i class="fa fa-angle-right" aria-hidden="true"></i>LabVIEW互操作组件,在C#和LabVIEW之间交换图像</a></li>
                                                <li><a href="https://www.ijwell.com/a/W5iYjo.html" title="将文本文件保存到.npy文件"><i class="fa fa-angle-right" aria-hidden="true"></i>将文本文件保存到.npy文件</a></li>
                                                <li><a href="https://www.ijwell.com/a/h0EbOL.html" title="将套接字IO注册到sveltekit的VITE"><i class="fa fa-angle-right" aria-hidden="true"></i>将套接字IO注册到sveltekit的VITE</a></li>
                                                <li><a href="https://www.ijwell.com/a/v5WMh7.html" title="反应使用效果导致无限循环"><i class="fa fa-angle-right" aria-hidden="true"></i>反应使用效果导致无限循环</a></li>
                                                <li><a href="https://www.ijwell.com/a/W3ud1o.html" title="Android的AOSP构建失败-10.0.0_r39的Build_Image.py"><i class="fa fa-angle-right" aria-hidden="true"></i>Android的AOSP构建失败-10.0.0_r39的Build_Image.py</a></li>
                                                <li><a href="https://www.ijwell.com/a/Qr3Jbh.html" title="计算迭代工具的第n个结果。product()"><i class="fa fa-angle-right" aria-hidden="true"></i>计算迭代工具的第n个结果。product()</a></li>
                                                <li><a href="https://www.ijwell.com/a/X3x5xK.html" title="Web Fetch API(等待FETCH完成,然后执行下一条指令)"><i class="fa fa-angle-right" aria-hidden="true"></i>Web Fetch API(等待FETCH完成,然后执行下一条指令)</a></li>
                                                <li><a href="https://www.ijwell.com/a/N6xrAX.html" title="2D正方形到矩形的碰撞检测和操作(物理)"><i class="fa fa-angle-right" aria-hidden="true"></i>2D正方形到矩形的碰撞检测和操作(物理)</a></li>
                                                <li><a href="https://www.ijwell.com/a/x2NKcK.html" title="创建自定义简单Mapi DLL,执行时失败"><i class="fa fa-angle-right" aria-hidden="true"></i>创建自定义简单Mapi DLL,执行时失败</a></li>
                                            </ul>
                </div>
            </div>

            <div class="right-card">
                <div class="right-card-main">
                    <div class="right-card-title">猜您喜欢</div>
                    <ul class="right-recommended-list">
                                            </ul>
                </div>
            </div>
            <div class="right-card">
                <!-- ad right_1 -->
                <script>pr('3', "right_1");</script>
            </div>
            <div class="right-card">


            </div>
            <div class="right-card">
                <!-- ad right_2 -->
                <script>pr('3', "right_2");</script>
            </div>
        </div>
    </div>
</div>
<script>
    if (typeof bodyUrlInfo !== "undefined") {
        setTimeout(function() {
            for (let i = 0; i < bodyUrlInfo.length; i++) {
                let link="/gourl/index.php?u="+bodyUrlInfo[i];
                $("h3[data-id='"+i+"']").wrap('<a href="'+link+'" target="_blank"></a>');

            }
        }, 5000); // 5000毫秒即为5秒
    }
</script>
<div class="container-fluid cl-footer">
    <div class="container">
        <!--        <p class="cl-copyright">友情链接交换请联系QQ330946442</p>-->
        <p class="cl-copyright"> <a href="https://beian.miit.gov.cn/" target="_blank">蜀ICP备2022007388号-4</a> </p></p>
    </div>
</div>
<script src="https://static.ijwell.com/dev/static/jquery/jquery.min.js"></script>
<script src="https://static.ijwell.com/dev/static/bootstrap/js/bootstrap.min.js"></script>
<script src="https://static.ijwell.com/dev/static/highlight/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
    //百度统计
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?f075d666e7199caf359981fa25394118";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>
</html>