basic web auth and verification

Ralf Schönian ralf at schoenian-online.de
Tue Oct 23 09:55:31 CEST 2007


scripteaze at gmail.com schrieb:
> Trying to figure out how to add login verfication. I believe it is
> logging me in, but theres no way to really tell..any ideas? or
> tutorials out there that can exaplain this to me?
> 
> Thanks
> 
> import urllib,urllib2,cookielib
> 
> passlst = open(passfile, 'r').readlines()
> url="http://somesite"
> cj = cookielib.LWPCookieJar()
> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
> urllib2.install_opener(opener)
> TheForm = urllib.urlencode({"username":"myid","password":"mypas"})
> request = urllib2.Request(url, TheForm)
> result = urllib2.urlopen(request)
> html=result.read()
> result.close()
> 

I had the same trouble. There was a redirect after login a had to 
follow. Look at the following code.

     def login(self):
         ''' Log into web site. '''

         self._br.set_handle_redirect(True)
         cj = CookieJar()
         self._br.set_cookiejar(cj)
         self._br.open(Config.urlLogin)
         self._br.select_form(name='login')

         self._br['session_key'] = Config.username
         self._br['session_password'] = Config.password
         response=self._br.submit()
         self._br.set_response(response)
         for link in self._br.links(url_regex="www.somesite.com"):
             self._br.follow_link(link)
         if 'Sign In' in self._br.title():
             raise ValueError('Wrong password')


Regards,
Ralf Schoenian



More information about the Python-list mailing list