2015-01-29 5 views
0

편의를 위해 자동으로 웹 액세스 인증 페이지에 로그인하는 약간의 파이썬 스크립트를 만들려고합니다 (컴퓨터가 네트워크에서 연결이 끊어 질 때마다 로그인이 나타납니다).Python을 사용하여 간단한 웹 액세스 로그인에 로그인하는 방법은 무엇입니까?

login

나의 시도는 지금까지 모듈 기계화를 사용하고 있지만,이 내 표준 브라우저에서 사라지는 로그인을 초래하지 않는 실행하고있다 :이 로그인을 얻을 수있는 방법

import mechanize 
browser = mechanize.Browser() 
browser.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")] 
browser.open("https://controller.mobile.lan/101/portal/") 
browser.select_form(name="logonForm") 
browser["login"] = "myUsername" 
browser["password"] = "myPasscode" 
browser.submit() 
print browser.title() 

파이썬으로 작업 하시겠습니까? 어딘가에을 제출 데이터를 형성

<form name="logonForm" style="display:none"> 
    <!-- Logon Form --> 
    <div id="logonForm_subscriptionChoice_top_title_block" class="subtitle"> 
     <span id="logonForm_subscriptionChoice_top_title_text">YOU ALREADY HAVE YOUR LOGIN</span> 
    </div> 
    <div id="logonForm_auth_modes_block" style="display:none"> 
    <table class="hoverLink"><tr> 
<td> 
       <div id="logonForm_shibboleth_authentication_button"> 
        <img src="./resources/_images/shibboleth.png" height="30px"><br><span id="logonForm_shibboleth_text">Utilisez vos identifiants institutionnels</span> 
       </div> 
      </td> 
      <td> 
       <div id="logonForm_standard_authentication_button"> 
        <img src="./resources/_images/ticket.png" height="30px"><br><span id="logonForm_ticket_text">Utilisez un ticket de connexion</span> 
       </div> 
      </td> 
     </tr></table> 
</div> 
    <div id="logonForm_logon_block"> 
     <table> 
<tr id="logonForm_logon_block_credentials"> 
<td class="label"> 
        <span id="logonForm_login_text">LOGIN</span><br><input type="text" name="login" autocomplete="on"> 
</td> 
       <td class="label"> 
        <span id="logonForm_password_text">PASSWORD</span><br><input type="password" name="password" autocomplete="on"> 
</td> 
       <td> 
        <button type="submit" id="logonForm_connect_button"><span><img src="./resources/_images/auth_button.png" height="35px"></span></button> 
       </td> 
      </tr> 
<tr id="logonForm_policy_block"> 
<!-- Check Box Confirm (Visible status depends on configuration option) --><td colspan="3"> 
        <br><input type="checkbox" name="policy_accept">&nbsp; 
        <span id="logonForm_policy_text"></span> 
       </td> 
      </tr> 
</table> 
</div> 
    <br><button type="button" id="logonForm_authentication_form_back_button" style="display:none">Retour</button> 
    <div id="logonForm_subscriptionChoice_block"> 
     <br><div class="subtitle"> 
      <span id="logonForm_subcribe_bottom_title_text">NOT A LOGIN YET ?</span> 
     </div> 
     <br><div id="logonForm_subscriptionChoice_first_double_insert_block"> 
      <table class="hoverLink"><tr> 
<td></td> 
<td></td> 
</tr></table> 
</div> 
     <div id="logonForm_subscriptionChoice_second_double_insert_block"> 
      <table class="hoverLink"><tr> 
<td></td> 
<td></td> 
</tr></table> 
</div> 
     <div id="logonForm_subscriptionChoice_single_insert_block"> 
      <table class="hoverLink"><tr><td></td></tr></table> 
</div> 
    </div> 
</form> 

답변

1

:

여기에 내가 생각하는 로그인 페이지의 HTML 관련 섹션입니다. 어떤 방법을 사용하는지 알아야합니다. 그것을하지 않습니다 기계화, 그 양식을 제출 자바 스크립트를 사용하는 경우 있음을

response = requests.post("https://controller.mobile.lan/101/portal/", data={'login': "username", 'password': "password") 
print response.read() # Dumps the whole webpage after. 

참고하면 얻을해야합니다 : 당신이 발견 한 후처럼 한 줄을 수행 할 requests 라이브러리를 사용할 수 있습니다 실제로 자바 스크립트를 틱하게 만듭니다. Mechanize의 FAQ (here)는 자바 스크립트를 사용하지 않기 때문에 코드에서 직접 에뮬레이트해야합니다.

편집 :

import sys 
from PyQt4.QtGui import QApplication 
from PyQt4.QtCore import QUrl 
from PyQt4.QtWebKit import QWebPage 

# Set vars here for convenience 
username = "myUsername" 
password = "myPassword" 

class HeadlessBrowser(QWebPage): 
    def __init__(self, url): 
     self.app = QApplication(sys.argv) 
     super(HeadlessBrowser, self).__init__() 
     self.loadFinished.connect(self.login) 
     self.mainFrame().load(QUrl(url)) 
     self.app.exec_(); 

    def login(self): 
     doc = self.mainFrame().documentElement() 
     user = doc.findFirst("input[name=login]") 
     pwd = doc.findFirst("input[name=password]") 
     button = doc.findFirst("button[id=logonForm_connect_button]") 

     user.setAttribute("value", username) 
     pwd.setAttribute("value", password) 
     button.evaluateJavaScript("this.click()") 
     # Uncomment if the button click above is not enough 
     #form = doc.findFirst("form[name=logonForm]") 
     #form.evaluateJavaScript("this.submit()") 
     self.app.quit() 

page = HeadlessBrowser("http://localhost/~iskren/headlesstest.html") 
html = page.mainFrame().toHtml() 

내가 테스트에 사용 http://localhost/~iskren/headlesstest.html의 내용 : 당신이 PyQt4 주위에 거짓말을, 또는 설치할 수 있습니다 경우이 같은 '헤드리스'브라우저를 사용할 수 있습니다

<html> 
<body> 
<form name="logonForm"> 
     <input type="text" name="login"/> 
     <input type="password" name="password"/> 
     <button type="submit" id="logonForm_connect_button">Click me!</button> 
</form> 
</body> 
</html> 
+0

도움 주셔서 감사합니다. 그래, 기계화가이 경우 작동하도록 추가 맞춤 코드가 필요하다는 것을 알 수있다 (페이지는 자바 스크립트를 사용한다). 지금은 [Selenium] (http://www.seleniumhq.org/)의 Python 바인딩을 사용하고 있습니다. 이것은 직관적이지만 세련되지 않습니다 (그래픽 작업을하기 위해 Firefox를로드하는 등). – d3pd

+0

GUI가없는 접근법에 대한 업데이트 된 답변을 확인하십시오. –