How can I log into a simple web access login using Python?
Question: I'm trying to create a little Python script that'll log into a web access
authentication page for me automatically for the purposes of convenience (the
login appears each time the computer is disconnected from the network).

![login](http://i.stack.imgur.com/bnItb.png)

My attempt so far has been to use the module mechanize, but running this
doesn't result in the login vanishing from my standard browser:

    
    
    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()
    

How can I get this login to work in Python?

Here's what I think is the relevant section of the HTML of the login page:

    
    
    <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>
    


Answer: That form submits data somewhere. You need to find out where and what method
it uses. After you find out, you can use the `requests` library to do a one-
liner, like:

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

Note that if that form uses javascript for submission, mechanize won't do it
and you'll have to get something that actually makes javascript tick.
Mechanize's FAQ
([here](http://wwwsearch.sourceforge.net/mechanize/faq.html#general))
specifies that it doesn't do javascript and you have to emulate it in your own
code.

Edit: If you have PyQt4 lying around, or can install it, you can use a
'headless' browser, like this:

    
    
    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()
    

And the contents of `http://localhost/~iskren/headlesstest.html` that I used
for testing:

    
    
    <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>
    

