2016-04-29 18 views
0

私はlocalhost\administrationでログインページを持っていると私は、ログインしてこのコードを使用しています:PythonでWebページにログインするにはどうすればよいですか?

import urllib 
import http.cookiejar as ckj 
lg = {'nick':'john','pass':'password'} 
url = 'http://localhost/administration/' 
cj = ckj.CookieJar() 
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) 
lgdata = urllib.parse.urlencode(lg).encode("utf-8") 
opener.open(url,lgdata) 
res = opener.open(url) 
print(res.read()) 

ページはPHPコードのみです。 今、私はこのページにログインしたいですlocalhost:8080/pentaho/Login、しかし、このページはログインのためにJavaScriptを使用しています。どのようにデータをページに送るべきですか?&

私はurl = localhost:8080/pentaho/Loginlg = {'j_username':'username','j_password':'password'}を持っている必要があることを理解していますが、これはアクションに送信する必要があるすべての情報ではありません。ここで

は、ログインフォームのHTMLコードです:

<form name="login" id="login" action="j_spring_security_check" method="POST" 
onkeyup="if(window.event && window.event.keyCode && window.event.keyCode==13) 
{var buttonToClick = document.getElementById('loginbtn'); 
if(buttonToClick){ buttonToClick.click();}}"> 
    <input id="j_username" name="j_username" type="text" placeholder="" autocomplete="off"> 
    <input id="j_password" name="j_password" type="password" placeholder="" autocomplete="off"> 
    <button type="submit" id="loginbtn" class="btn">Login</button> 
    <input type="hidden" name="locale" value="en_US"> 
</form> 

そして、ここでは、ページのjavascriptのjavascriptのを見ると

<script type="text/javascript"> 
    function toggleEvalPanel() { 
    var evaluationPanel = $("#evaluationPanel"); 
    evaluationPanel.toggleClass("afterSlide"); 
    $("#eval-arrow").toggleClass("closed"); 
    } 
    function bounceToReturnLocation() { 
    // pass 
    var locale = document.login.locale.value; 
    var returnLocation = 'http\x3A\x2F\x2Flocalhost\x3A8080\x2Fpentaho\x2Findex.jsp'; 
    if (returnLocation != '' && returnLocation != null) { 
     window.location.href = returnLocation; 
    } else { 
     window.location.href = window.location.href.replace("Login", "Home") + "?locale=" + locale; 
    } 
    } 
    function doLogin() { 
    // if we have a valid session and we attempt to login on top of it, the server 
    // will actually log us out and will not log in with the supplied credentials, you must 
    // login again. So instead, if they're already logged in, we bounce out of here to 
    // prevent confusion. 
    if (false) { 
     bounceToReturnLocation(); 
     return false; 
    } 
    jQuery.ajax({ 
     type: "POST", 
     url: "j_spring_security_check", 
     dataType: "text", 
     data: $("#login").serialize(), 
     error:function (xhr, ajaxOptions, thrownError){ 
     if (xhr.status == 404) { 
      // if we get a 404 it means login was successful but intended resource does not exist 
      // just let it go - let the user get the 404 
      bounceToReturnLocation(); 
      return; 
     } 
     //Fix for BISERVER-7525 
     //parsereerror caused by attempting to serve a complex document like a prd report in any presentation format like a .ppt 
     //does not necesarly mean that there was a failure in the login process, status is 200 so just let it serve the archive to the web browser. 
     if (xhr.status == 200 && thrownError == 'parsererror') { 
      document.getElementById("j_password").value = ""; 
      bounceToReturnLocation(); 
      return; 
     } 
     // fail 
     $("#loginError").show(); 
     $("#loginError button").focus(); 
     }, 
     success:function(data, textStatus, jqXHR){ 
     if (data.indexOf("j_spring_security_check") != -1) { 
      // fail 
      $("#loginError").show(); 
      $("#loginError button").focus(); 
      return false; 
     } else { 
      document.getElementById("j_password").value = ""; 
      bounceToReturnLocation(); 
     } 
     } 
    }); 
    return false; 
    } 
    function loginAs (username, password) { 
    $("#j_username").attr("value", username); 
    $("#j_password").attr("value", password); 
    doLogin(); 
    } 
    $(document).ready(function(){ 
    $("#login").submit(doLogin); 
    if (false) { 
     bounceToReturnLocation(); 
    } 
    $("#login-background").fadeIn(1000, function() { 
     $("#login-logo").addClass("afterSlide"); 
     $("#animate-wrapper").addClass("afterSlide"); 
     $("#j_username").focus(); 
     $("#login-footer").addClass("afterSlide"); 
    }); 
    }); 
</script> 

答えて

1

あなたはAJAXリクエストを行う必要があります。

import requests 

headers = { 
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0', 
    'Accept': 'application/json, text/javascript, */*; q=0.01', 
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 
    'X-Requested-With': 'XMLHttpRequest' 
} 

login_url = '/pentaho/j_spring_security_check' 
s = requests.Session() 
data = {'j_username': YOURUSERNAME, 'j_password': YOURPASSWORD} 
s.post(login_url, data=login_data) 

content = s.post(url, data=data, headers=headers) 
print content 
+0

お返事ありがとうございます、私はこれを試しましたが、実際には動作していません。私が返すのはです。つまり、サーバー上の複雑なドキュメントにアクセスしようとしています – Andrew

0

である、あなたはhttp://localhost:8080/pentaho/j_spring_security_checkPOSTリクエストを送信したいですペイロード付きj_username=<username>&j_password=<password>

セキュリティチェックの場所は、フォーミュラの場所によって異なります。同じフォルダの場合と同様に、そうでない場合はURLが変更されます。

関連する問題