Need Pattern For Logging Into A Website

T

Tim Daneliuk

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

Steve Petrie

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 (e-mail address removed)

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

Tim Daneliuk

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 (e-mail address removed)

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

Tim Daneliuk

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

Jan WÄ…sak

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top