Entering username & password automatically using urllib.urlopen

Discussion in 'Python' started by rodrigo, Oct 14, 2007.

  1. rodrigo

    rodrigo Guest

    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
     
    rodrigo, Oct 14, 2007
    #1
    1. Advertising

  2. 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/articles/authentication.shtml

    BTW, use urllib2.

    Diez
     
    Diez B. Roggisch, Oct 14, 2007
    #2
    1. Advertising

  3. rodrigo

    Guest

    On Oct 13, 11:41 pm, rodrigo <> wrote:
    > 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, 2007
    #3
  4. > 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
     
    Ryan Ginstrom, Oct 15, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xu, C.S.
    Replies:
    5
    Views:
    520
    John J. Lee
    Sep 17, 2003
  2. Replies:
    3
    Views:
    2,117
    Cousin Stanley
    Dec 31, 2003
  3. Chris
    Replies:
    0
    Views:
    1,114
    Chris
    Jul 10, 2005
  4. John Nagle
    Replies:
    1
    Views:
    555
    John J. Lee
    Dec 27, 2006
  5. Eric
    Replies:
    1
    Views:
    361
    Alexey Smirnov
    Apr 2, 2007
Loading...

Share This Page