Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37646 > unrolled thread

Need Pattern For Logging Into A Website

Started byTim Daneliuk <tundra@tundraware.com>
First post2013-01-24 19:29 -0600
Last post2013-01-28 00:57 -0800
Articles 8 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Need Pattern For Logging Into A Website Tim Daneliuk <tundra@tundraware.com> - 2013-01-24 19:29 -0600
    Re: Need Pattern For Logging Into A Website Steve Petrie <spetrie@gmail.com> - 2013-01-25 08:01 -0800
      Re: Need Pattern For Logging Into A Website Tim Daneliuk <tundra@tundraware.com> - 2013-01-25 10:18 -0600
        Re: Need Pattern For Logging Into A Website Michael Torrie <torriem@gmail.com> - 2013-01-25 12:18 -0700
          Re: Need Pattern For Logging Into A Website Tim Daneliuk <tundra@tundraware.com> - 2013-01-25 18:15 -0600
            Re: Need Pattern For Logging Into A Website Michael Torrie <torriem@gmail.com> - 2013-01-25 23:53 -0700
              Re: Need Pattern For Logging Into A Website Tim Daneliuk <tundra@tundraware.com> - 2013-01-26 14:00 -0600
    Re: Need Pattern For Logging Into A Website Jan Wąsak <jhnwsk@gmail.com> - 2013-01-28 00:57 -0800

#37646 — Need Pattern For Logging Into A Website

FromTim Daneliuk <tundra@tundraware.com>
Date2013-01-24 19:29 -0600
SubjectNeed Pattern For Logging Into A Website
Message-ID<gp39t9-9rk1.ln1@ozzie.tundraware.com>
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@tundraware.com
PGP Key:         http://www.tundraware.com/PGP/

[toc] | [next] | [standalone]


#37677

FromSteve Petrie <spetrie@gmail.com>
Date2013-01-25 08:01 -0800
Message-ID<3f55e471-8657-4cad-96cd-96e9e2e01a9d@googlegroups.com>
In reply to#37646
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@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()
...

[toc] | [prev] | [next] | [standalone]


#37678

FromTim Daneliuk <tundra@tundraware.com>
Date2013-01-25 10:18 -0600
Message-ID<qrnat9-lli2.ln1@ozzie.tundraware.com>
In reply to#37677
On 01/25/2013 10:01 AM, Steve Petrie wrote:
> 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@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()
> ...
>

Thanks.

-- 
----------------------------------------------------------------------------
Tim Daneliuk     tundra@tundraware.com
PGP Key:         http://www.tundraware.com/PGP/

[toc] | [prev] | [next] | [standalone]


#37689

FromMichael Torrie <torriem@gmail.com>
Date2013-01-25 12:18 -0700
Message-ID<mailman.1053.1359142676.2939.python-list@python.org>
In reply to#37678
On 01/25/2013 09:18 AM, Tim Daneliuk wrote:
> On 01/25/2013 10:01 AM, Steve Petrie wrote:
>> On Thursday, January 24, 2013 8:29:51 PM UTC-5, Tim Daneliuk
>> wrote: The mechanize module
>> (http://wwwsearch.sourceforge.net/mechanize/) might be a place to
>> start.  I've done something similar with code like this:
> Thanks.

I've had good luck using urllib2 and a cookiejar.  Just post your login
credentials against the login url, keep all the cookies and then make
your other requests using that cookiejar.  Besides the Python standard
library docs on urllib2 and cookielib, here's an article that gives an
overview of how to use it:

http://www.techchorus.net/using-cookie-jar-urllib2

This technique has the advantage of not requiring anything outside of
the standard library.

[toc] | [prev] | [next] | [standalone]


#37703

FromTim Daneliuk <tundra@tundraware.com>
Date2013-01-25 18:15 -0600
Message-ID<5pjbt9-lli2.ln1@ozzie.tundraware.com>
In reply to#37689
On 01/25/2013 01:18 PM, Michael Torrie wrote:
> On 01/25/2013 09:18 AM, Tim Daneliuk wrote:
>> On 01/25/2013 10:01 AM, Steve Petrie wrote:
>>> On Thursday, January 24, 2013 8:29:51 PM UTC-5, Tim Daneliuk
>>> wrote: The mechanize module
>>> (http://wwwsearch.sourceforge.net/mechanize/) might be a place to
>>> start.  I've done something similar with code like this:
>> Thanks.
>
> I've had good luck using urllib2 and a cookiejar.  Just post your login
> credentials against the login url, keep all the cookies and then make
> your other requests using that cookiejar.  Besides the Python standard
> library docs on urllib2 and cookielib, here's an article that gives an
> overview of how to use it:
>
> http://www.techchorus.net/using-cookie-jar-urllib2
>
> This technique has the advantage of not requiring anything outside of
> the standard library.
>

Does it handle self-signed SSL certs?

-- 
----------------------------------------------------------------------------
Tim Daneliuk     tundra@tundraware.com
PGP Key:         http://www.tundraware.com/PGP/

[toc] | [prev] | [next] | [standalone]


#37714

FromMichael Torrie <torriem@gmail.com>
Date2013-01-25 23:53 -0700
Message-ID<mailman.1068.1359183201.2939.python-list@python.org>
In reply to#37703
On 01/25/2013 05:15 PM, Tim Daneliuk wrote:
> Does it handle self-signed SSL certs?

No idea.  you'd have to try it.

[toc] | [prev] | [next] | [standalone]


#37734

FromTim Daneliuk <tundra@tundraware.com>
Date2013-01-26 14:00 -0600
Message-ID<l7pdt9-lli2.ln1@ozzie.tundraware.com>
In reply to#37714
On 01/26/2013 12:53 AM, Michael Torrie wrote:
> On 01/25/2013 05:15 PM, Tim Daneliuk wrote:
>> Does it handle self-signed SSL certs?
>
> No idea.  you'd have to try it.
>

OK, thanks for the pointer.

-- 
----------------------------------------------------------------------------
Tim Daneliuk     tundra@tundraware.com
PGP Key:         http://www.tundraware.com/PGP/

[toc] | [prev] | [next] | [standalone]


#37798

FromJan Wąsak <jhnwsk@gmail.com>
Date2013-01-28 00:57 -0800
Message-ID<6060b71d-24b8-406d-8feb-d27e46884c03@googlegroups.com>
In reply to#37646
On Friday, January 25, 2013 2:29:51 AM UTC+1, 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,
> 

Hello Tim,

I think you may also want to take a look at python requests.
http://docs.python-requests.org/en/latest/user/advanced/

From my experience they do a much nicer job than urllib and anything I've tried.
You will probably get what you want out of them easily and in no time.

-- 
cheers,
john

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web