Simple httplib problem

Max M maxm at mxm.dk
Tue Jan 7 12:12:28 CET 2003


Afanasiy wrote:
> Can someone tell me what I am doing wrong here?
> 


You are not using ClentCookie ;-)

http://wwwsearch.sourceforge.net/ClientCookie/


With it you will be able to write code like the following, wich makes 
the process pretty painless (untested):


import urllib, urllib2, ClientCookie

class SiteBrowser:

     def __init__(self, url, **form_data):
         # form fields, including hidden ones
         data = urllib.urlencode(form_data)
         # login page
         request = urllib2.Request(url, data)
         result = ClientCookie.urlopen(request)
         self.content = ''

     def browse(self, url):
         # Find a password protected page
         req = urllib2.Request(url)
         res = ClientCookie.urlopen(req)
         self.content = res.read()
         res.close()



if __name__=='__main__':

     usr = 'user'
     pwd = 'secret'
     form_name = 'login_form', # hidden field!
     button = 'ok'
     url = 'http://www.somewhere.com/loginform.asp'

     browser = SiteBrowser(
             url, USERNAME=usr, PASSWORD=pwd, FORM_NAME=form_name, ok='')
     browser.browse('http://www.somewhere.com/protected_page.asp')
     print browser.content


-- 

hilsen/regards Max M

http://www.futureport.dk/
Fremtiden, videnskab, skeptiscisme og transhumanisme





More information about the Python-list mailing list