#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    1
    Rep Power
    0

    Python - easy question


    Hello, I'm pretty new to Python and have the following question before I proceed with studying it further:

    The question is about openurl function.

    Let's say I want my python script to access password protected page, how would I do that?

    Will it open IE and try to access it or does it access the page "internally".

    Sample code would be awesome.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi Pasha,

    This rather untidy example used to work fine but with the recent update to Devsheds forums it could very well be broken

    Code:
    #!/usr/bin/env python
    
    import urllib, urllib2
    
    def update(link, username, password, email, signature):
    	
    	param1 = urllib.urlencode({'action': 'login', 'username': username, 'password': password}) 
    	param2 = urllib.urlencode({'action': 'updateprofile', 'email': email, 'emailconfirm': email, 'signature': signature}) 
     
    	cookie = urllib.urlopen(link, param1).info()['Set-Cookie'] 
    	cookie = [c for c in cookie.split() if c.endswith(';')][::2]
     
    	urllib.urlopen(urllib2.Request(link, param2, {'Cookie': ''.join(cookie)}))
    	
    update('http://forums.devshed.com/member.php', 'netytan', 'passwd', 'netytan@somewhere.com', 'www.python.org')
    If you hadn't guessed it already, this script updates a users signature. But it does show how you can pass arguments to pages etc.

    Not for the weak heated and it usually takes some time to figure out jsut what a page needs. especially if you can't look at the source code!

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    I used this in a batch file:
    Code:
    import urllib
    class myopener(urllib.FancyURLopener):
        def prompt_user_passwd(self,host,realm):
            return ("username","password")
        
    sess = myopener()
    
    f = sess.open("http://somewhere/index.html")
    page = f.read()
    just put in a valid username and password. You can of course modify prompt_user_passwd to use raw_input

    Grim

IMN logo majestic logo threadwatch logo seochat tools logo