Need Pattern For Logging Into A Website

Steve Petrie spetrie at gmail.com
Fri Jan 25 11:01:39 EST 2013


On Thursday, January 24, 2013 8:29:51 PM UTC-5, Tim Daneliuk wrote:
> I need to write a Python script to do the following:
> 
> 
> 
>    - Connect to a URL and accept any certificate - self-signed or authoritative
> 
>    - Provide login name/password credentials
> 
>    - Fill in some presented fields
> 
>    - Hit a "Submit" button
> 
> 
> 
> Why?  Because I don't want to have to start a browser and do this
> 
> interactively every time I authenticate with a particular server.
> 
> I want to do this at the command line with no interactive intervention.
> 
> 
> 
> I know Python pretty well.  I don't quite know how to do this and
> 
> was hoping someone had a simple pattern they could share for
> 
> doing this.
> 
> 
> 
> TIA,
> 
> -- 
> 
> ----------------------------------------------------------------------------
> 
> Tim Daneliuk     tundra at tundraware.com
> 
> PGP Key:         http://www.tundraware.com/PGP/

The mechanize module (http://wwwsearch.sourceforge.net/mechanize/) might be a place to start.  I've done something similar with code like this:

response = mechanize.urlopen(login_form_url)
forms = mechanize.ParseResponse(response, backwards_compat=False)
response.close()
form = forms[0]                               # might be more than one, though
                                              # fill the form
form.set_value(username, name='userName')
form.set_value(password, name='password')
                                              # set headers - user-agent, etc.
login_request = form.click()
login_response = mechanize.urlopen(login_request)
login_response_content = login_response.read()
...



More information about the Python-list mailing list