Entering username & password automatically using urllib.urlopen

R

rodrigo

I am trying to retrieve a password protected page using:

get = urllib.urlopen('http://password.protected.url"').read()

While doing this interactively, I'm asked for the username, then the
password at the terminal.
Is there any way to do this non-interactively? To hardcode the user/
pass into the script so I can get the page automatically?

(This is not a cracking attempt, I am trying to retrieve a page I have
legitimate access to, just doing it automatically when certain
conditions are met.)

Thanks,

Rodrigo
 
D

Diez B. Roggisch

rodrigo said:
I am trying to retrieve a password protected page using:

get = urllib.urlopen('http://password.protected.url"').read()

While doing this interactively, I'm asked for the username, then the
password at the terminal.
Is there any way to do this non-interactively? To hardcode the user/
pass into the script so I can get the page automatically?

(This is not a cracking attempt, I am trying to retrieve a page I have
legitimate access to, just doing it automatically when certain
conditions are met.)

Is that HTTP-auth? Then this might help:

http://www.voidspace.org.uk/python/articles/authentication.shtml

BTW, use urllib2.

Diez
 
B

byte8bits

I am trying to retrieve a password protected page using:

get = urllib.urlopen('http://password.protected.url"').read()

While doing this interactively, I'm asked for the username, then the
password at the terminal.
Is there any way to do this non-interactively? To hardcode the user/
pass into the script so I can get the page automatically?

(This is not a cracking attempt, I am trying to retrieve a page I have
legitimate access to, just doing it automatically when certain
conditions are met.)

Thanks,

Rodrigo

The pexpect module works nicely for automating tasks that normally
require user interaction.
 
R

Ryan Ginstrom

On Behalf Of rodrigo
I am trying to retrieve a password protected page using:

get = urllib.urlopen('http://password.protected.url"').read()

I would suggest looking at mechanize.
http://wwwsearch.sourceforge.net/mechanize/

from mechanize import Browser

USERNAME = "user"
PASSWORD = "secret"
LOGIN_PAGE = "http://password.protected.url/"

browser = Browser()
browser.open( LOGIN_PAGE )

# log in
browser.select_form( nr=0 ) # Assuming log in form is first form on the page
# Check the form for the actual field names...
browser['user'] = USERNAME
browser['pass'] = PASSWORD
browser.submit()

# Content goodness follows...

##
Of course, this assumes that the site doesn't use some kind of JavaScript
trickery to prevent automation like the above. In that case, you'd have to
use something like PAMIE
http://sourceforge.net/projects/pamie/

HTH,
Ryan Ginstrom
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top