467,077 Members | 1,033 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Entering username & password automatically using urllib.urlopen

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

Oct 14 '07 #1
  • viewed: 18220
Share:
3 Replies
rodrigo schrieb:
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/a...tication.shtml

BTW, use urllib2.

Diez
Oct 14 '07 #2
On Oct 13, 11:41 pm, rodrigo <rodrigo...@gmail.comwrote:
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.

Oct 14 '07 #3
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

Oct 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by MCollins@seminolecountyfl.gov | last post: by
1 post views Thread by siddharth_jain_1@rediffmail.com | last post: by
4 posts views Thread by kvicky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.