[Cubicweb] [PROVENANCE INTERNET] Re: Apache authentication: which CubicWeb API?

Alexandre Fayolle afayolle.ml at free.fr
Sat Oct 12 18:03:55 CEST 2013


On 09/10/2013 09:33, Sylvain Thénault wrote:
> On 08 octobre 22:38, Nicolas Chauvat wrote:
>> Hi Dimitri,
>>
>> On Tue, Oct 08, 2013 at 09:41:43PM +0200, Dimitri Papadopoulos Orfanos wrote:
>>> How to programatically access the URL from a client program, not a
>>> Web browser?
>>
>> pydoc urllib2 ?
>>
>>> Are cookies needed?
>>
>> Only if your site requires cookies to log in and the information you
>> are looking for is not available to anonymous users.
>>
>> http://docs.python.org/2/library/cookielib.html
> 
> You need cookie handling if you want a session, so almost always when you start
> doing more than a single HTTP request. Extract from
> cubicweb.server.sources.datafeed:
> 
>   _OPENER = urllib2.build_opener()
>   # this is for Kerberos authentication support
>   try:
>       from logilab.common import urllib2ext
>       _OPENER.add_handler(urllib2ext.HTTPGssapiAuthHandler())
>   except ImportError: # python-kerberos not available
>       pass
>   # and this for cookies
>   _OPENER.add_handler(urllib2.HTTPCookieProcessor(CookieJar()))
> 
>  
> Then you can use _OPENER.open(url), authenticating the first time using __login
> and __password and next request will have the session cookie set.
> 

Side note : for a much nicer API than urllib2, give a try to python
request (http://docs.python-requests.org/en/latest/)

Alexandre



More information about the Cubicweb mailing list