Personal tools
You are here: Home Plone XP leocornus.django.ploneproxy Dependence on httplib2

Dependence on httplib2

Our current solutions will try to get cookies from Plone site programmaticly!  Or in another word, we need post the login form and collect the cookies by using Python script.  All should happen in backend!

>>> import httplib2
>>> import urllib
>>> http = httplib2.Http('.cache')
>>> login_url = 'http://internal.host.name/plone_site/login_form'
>>> headers = {}
>>> headers['Content-type'] = 'application/x-www-form-urlencoded'
>>> headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' 
>>> login_form = {}
>>> login_form['__ac_name'] = 'username'
>>> login_form['__ac_password'] = 'password'
>>> login_form['cookies_enabled'] = '1'
>>> login_form['js_enabled'] = '0'
>>> login_form['form.submitted'] = '1'
>>> body = urllib.urlencode(login_form)
>>> res,cont = http.request(login_url, 'POST', headers=headers, body=body)
>>> res['set-cookie']
'__ac="ddeiskdeisljSEI-deisl23_ESL245KD"; Path=/'
>>> import Cookie
>>> cookie = Cookie.SimpleCookie()
>>> cookie.load(res['set-cookie'])
>>> cookie.get('__ac').value
'ddeiskdeisljSEI-deisl23_ESL245KD'

SimpleCookie from Cookie module is used to parse the cookie and extract the value!

The User-Agent is very important for httplib2 to send a cookies enabled request!

References

Tracking History

When Who What Done
2010-05-25 08:33 Sean Chen a sample python script to use httplib2 to login a Plone site: post a request and collect the cookie! revision: r403
-- 1.0 Hours, 100.0% Done
2010-05-13 09:36 Sean Chen spent a lot time to figure out the tricky part about 'Uaer-Agent'! until now every thin is ready to go. I need come back to update this story.
-- 2.0 Hours, 75.0% Done
Document Actions