Python发布cookie请求

时间:2015-12-28 10:18:21

标签: python ssl cookies python-requests

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

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

使用我之前使用SSL的PBX这是一项简单的任务 但我无法弄清楚如何使用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>

1 个答案:

答案 0 :(得分:0)

第二个POST不会向服务器发布任何数据,而第一个POST可能应该是GET

另外,尝试在会话中将第一个请求设为GET,然后使用登录凭据执行POST。使用会话消除了您明确管理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;如果我的建议有效,你可以尝试删除它。