views:

133

answers:

2

Hello,

I am trying to log onto a website using Python. I have written the code to connect to the target but I need to login and select a button on the website and wait for a response. I have looked at the HTTP Protocol in Python and was thinking of using 'HTTPConnection.putrequest'. I am not sure how to do this, I have the code I have so far below:

def testHTTPS(self):

    c = httplib.HTTPSConnection(ip) 
    c.request("GET", "/") 
    response = c.getresponse() 
    self.assertEqual(response.status, 200) # '200' is success code
    conn.close()

And the code for the logon function on the website is:

<td align="right" id="lgn_userName"></td>
    <td><input type="text" class="button" name="username" id="username" size="24" maxlength="16" accesskey="u" tabindex="1" value=""/></td>
    </tr>

    <tr>
        <td align="right" id="lgn_userPwd"></td>
        <td><input type="password" class="button" name="password" id="password" size="24" maxlength="20" accesskey="p" tabindex="2" value=""/></td>
    </tr>

    <tr>
        <td align="right">&nbsp;</td>
        <td>
        <input type="submit" id="lgn_button" class="button" tabindex="3" accesskey="s" />
        </td>

Does anyone know how to go about this?

Thanks

+2  A: 

Yes, you use mechanize, which a sort of a "webbrowser" for Python. With it you can easily open web pages, find forms, fill in form values and submit the forms from Python. I use it (via Zopes testbrowser module) for testing web applications.

Lennart Regebro