使用python使用cgi脚本登录网页

时间:2013-05-17 05:37:48

标签: python login cgi

登录Web服务器不是我的专业领域,我希望自动执行登录Web服务器的任务,但我不确定如何格式化代码以实现此目标。我查看了其他页面,但无论我做了什么,我都得到了相同的回复。

网页摘要:

  <form name="form1" action="login.cgi" method="POST" onsubmit="encrypt();">
    <input type="hidden" name="Token" value="16">
    <tr height="15">
      <td><img src="/images/spacer.gif" alt="" height="15"></td>
    </tr>
    <tr height="32">
      <td valign="top">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="19"><img src="/images/spacer.gif" alt="" width="19"></td>
            <td width="100%"> </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr height="12">
      <td><img src="/images/spacer.gif" alt="" height="12"></td>
    </tr>
    <tr>
      <td height="1" valign="top">
        <table border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="20"><img src="/images/spacer.gif" alt="" width="20"></td>
            <td valign="top">
              <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td><img src="/images/spacer.gif" alt="" width="10" height="8"></td>
                  <td><img src="/images/spacer.gif" alt="" width="400" height="8"></td>
                </tr>
                <tr>
                  <td nowrap>
                    <b>Login:</b>
                  </td>
                  <td>
                    <input name="userid_w" type="text" size="50">
                    <input name="userid" type="hidden" value=""><img src="/images/spacer.gif" alt="" width="10"></td>
                </tr>
                <tr>
                  <td colspan="2" height="3"><img src="/images/spacer.gif" alt="" height="3"></td>
                </tr>
                <tr>
                  <td nowrap>
                    <b>Login Password:</b>
                  </td>
                  <td>
                    <input name="password_w" type="password" size="50">
                    <input name="password" type="hidden" value="">
                  </td>
                </tr>
                <tr height="3">
                  <td colspan="2"><img src="/images/spacer.gif" alt="" height="3"></td>
                </tr>
                <tr>
                  <td></td>
                  <td>
                    <input type="submit" value="Login">

....

    <input type="hidden" name="open" value="">
  </form>
</table>

无论我给出的userid_w或password_w值是什么,我从服务器获得的响应返回:

<html>  <head></head>  <body onload='document.form1.submit()'>    <form name='form1' method='POST' action='message.cgi' target='_top'>      <input type='hidden' name='title' value='MSG_TTL_COOKIEOFF'/>      <input type='hidden' name='messageID' value='MSG_COOKIEOFF'/>      <input type='hidden' name='buttonURL' value='Frame.cgi'/>      <input type='hidden' name='screen' value='all'/>   </form>  </body></html>

任何帮助都将不胜感激。

编辑:在页面上运行curl -l会给出:

<html>  <head></head>  <body onload='document.form1.submit()'>    <form name='form1' method='POST' action='/web/guest/en/websys/webArch/message.cgi' target='_top'>      <input type='hidden' name='title' value='MSG_TTL_COOKIEOFF'/>      <input type='hidden' name='messageID' value='MSG_COOKIEOFF'/>      <input type='hidden' name='buttonURL' value='Frame.cgi'/>      <input type='hidden' name='screen' value='all'/>  </form>  </body></html

这看起来像我的pythons脚本输出...

1 个答案:

答案 0 :(得分:5)

看着 <form name="form1" action="login.cgi" method="POST" onsubmit="encrypt();">可以假设数据在被发送到login.cgi之前被javascript片段修改。如果是这种情况,您有两种选择:

  • 使用selenium传递凭据并继续登录,然后获取会话Cookie并将其复制到您选择的http客户端(例如requests
  • 使用提供的凭据运行javascript解释器(例如python-spidermonkey)以获取发送的真实POST值,并使用它们与您选择的http客户端进行呼叫