带有请求的 Python post cookie

标签 python ssl cookies python-requests

这是我的登录页面(它是 Elastix PBX) 我想使用 headless 浏览器登录以检索报告。

无论我尝试了多少变化 我仍然无法访问。

我以前的 PBX 不支持 SSL,这是一个简单的任务 但我不知道如何使用 SSL 做到这一点。

这是我的 Python 代码:

import requests

payload = {'input_user': 'admin', 'input_pass': 'pass', 'action':'submit_login'}

r1 = requests.post('https://10.8.20.3/index.php', data=payload, verify=False)
r2 = requests.post('https://10.8.20.3/index.php', cookies=r1.cookies, verify=False)

print r2.text

我希望在打印 r2.text 时看到我的限制页面 但我实际上得到了相同的登录页面。

这是我的 HTML 登录页面:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Elastix - Login page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!--<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">-->
    <link rel="stylesheet" href="themes/elastixneo/login_styles.css">
    <script type='text/javascript' src='libs/js/jquery/jquery-1.8.3.min.js'></script>
<script type='text/javascript' src='libs/js/jquery/jquery-ui-1.8.24.custom.min.js'></script>
<script type='text/javascript' src='libs/js/jquery/jquery-upl-blockUI.js'></script>
<script type='text/javascript' src='libs/js/jquery/jquery-upl-colResizable-1.3.min.js'></script>
<script type='text/javascript' src='libs/js/jquery/jquery-upl-colorpicker.js'></script>
<script type='text/javascript' src='libs/js/jquery/jquery-upl-easing.1.3.js'></script>
<link rel='stylesheet' href='libs/js/jquery/widgetcss/colorpicker.css' />
<link rel='stylesheet' href='libs/js/jquery/css/redmond/jquery-ui-1.8.24.custom.css' />
  </head>
  <body>
    <form method="POST">
      <div id="neo-login-box">
        <div id="neo-login-logo">
          <img src="themes/elastixneo/images/elastix_logo_mini.png" width="200" height="62" alt="elastix logo" />
        </div>
        <div class="neo-login-line">
          <div class="neo-login-label">Username:</div>
          <div class="neo-login-inputbox"><input type="text" id="input_user" name="input_user" class="neo-login-input" /></div>
        </div>
        <div class="neo-login-line">
          <div class="neo-login-label">Password:</div>
          <div class="neo-login-inputbox"><input type="password" name="input_pass" class="neo-login-input" /></div>
        </div>
        <div class="neo-login-line">
          <div class="neo-login-label"></div>
          <div class="neo-login-inputbox"><input type="submit" name="submit_login" value="Submit" class="neo-login-submit" /></div>
        </div>
        <div class="neo-footernote"><a href="http://www.elastix.org" style="text-decoration: none;" target='_blank'>Elastix</a> is licensed under <a href="http://www.opensource.org/licenses/gpl-license.php" style="text-decoration: none;" target='_blank'>GPL</a> by <a href="http://www.palosanto.com" style="text-decoration: none;" target='_blank'>PaloSanto Solutions</a>. 2006 - 2015.</div>
        <br>

        <script type="text/javascript">
            $(document).ready(function() {
                 $("#neo-login-box").draggable();
            });
            document.getElementById("input_user").focus();
        </script>

      </div>
    </form>
  </body>
</html>

最佳答案

第二个 POST 不向服务器发送任何数据,第一个 POST 应该是 GET

此外,尝试在 session 中发出第一个请求 GET,然后使用登录凭据执行 POST。使用 session 消除了您显式管理 cookie 的需要。

import requests

url = 'https://10.8.20.3/index.php'
payload = {'input_user': 'admin', 'input_pass': 'pass', 'action':'submit_login'}

s = requests.session()
r = s.get(url, verify=False)
r = s.post(url, data=payload, verify=False)

print r.text

我不确定您是否需要设置verify=False;如果我的建议有效,您可以尝试将其删除。

关于带有请求的 Python post cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34491609/

相关文章:

java - 检查 X509 证书吊销

node.js - 不同客户端 Nodejs 的 session id 相同

javascript - 仅当通过该查询字符串访问时才保留 URL 上的查询字符串

python - sqlalchemy 回滚的原因

python - 在排序期间尝试引用列表时出现 IndexError

Python - for循环退出 'prematurely'

python - 如何在 Django 模型中编写方法来检索相关对象?

SSL 证书配置不正确

unix - Java/Websphere NoSuchProviderException : IBMCertPath 问题

安卓持久登录