2017-05-30 57 views
0

我試圖通過蟒蛇登錄到一個網站,但表單字段沒有名字僅僅是ID的處理JavaScript和Python

<ul> 
        <li id="unLi" class="unLi"><input class="text" id="userName" type="text" maxlength="15" /></li> 
        <li class="blank"></li> 
        <li id="pwLi" class="pwLi"><input class="text" id="pcPassword" type="password" maxlength="15"/></li> 
</ul> 

我試着提交表單下面的代碼

import requests 

params = {'userName':'sahil','pcPassword':'mypassword'} 
r = requests.post("http://192.168.1.2/",data=params) 
r.status_code 
print(r.text) 

調用一個JavaScript函數PCSubWin()這就像這樣

function PCSubWin() 
{ 
    if((httpAutErrorArray[0] == 2) || (httpAutErrorArray[0] == 3)) 
    { 
     if(true == CheckUserPswInvalid()) 
     { 
      var username = $("userName").value;    
      var password = $("pcPassword").value; 
      if(httpAutErrorArray[1] == 1) 
      { 
       password = hex_md5($("pcPassword").value); 
      }   
      var auth = "Basic "+ Base64Encoding(username + ":" + password); 
      document.cookie = "Authorization="+escape(auth)+";path=/"; 
      //location.href ="/userRpm/LoginRpm.htm?Save=Save"; 
      $("loginForm").submit(); 
      return true; 
     } 
     else 
     { 
      $("note").innerHTML = "NOTE:"; 
      $("tip").innerHTML = "Username and password can contain between 1 - 15 characters and may not include spaces."; 
     } 
    } 
    return false; 

現在我不是非常好的JavaScript,但我認爲代碼$("userName").value這個peice獲取用戶輸入的值,我的問題是我如何在提交我的憑據時調用此函數?我的代碼btw輸出的登錄頁面不是用戶主頁相同的代碼。

回答

1

你的要求是使用Basic HTTP Authentication所以你只需撥打該網站:

requests.get('http://192.168.1.2/userRpm/LoginRpm.htm', auth=('sahil', 'mypassword')) 

訪問受保護的資源。

+0

你能告訴我如何驗證我的登錄信息嗎?爲前例如我可以從瀏覽器抓取網頁網址,有什麼方法可以驗證我可以訪問它嗎? @moritzg – Sahil

+1

@Sahil通過查看[HTTP狀態代碼](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)。如果登錄不成功,服務器應該返回401或403。如果一切順利,你會得到一個'200'。 – moritzg

+0

即使使用錯誤憑證,它也會給我一個200響應@moritzg – Sahil