On Dec 13, 2011, at 12:28 PM, peter wrote:

> So in my example above. How does web2py's admin know I am logged in?
> Does it do this by accessing a cookie on my computer, if so how does
> it do that behind the scenes? Could urllib2.urlopen really handle
> this?

Ordinarily it knows you're logged in because:

1. When you first log in, remembers it in a session and returns a cookie to 
your browser.

2. On subsequent requests, your browser includes that cookie, and web2py uses 
it to find the session (and its login state). (That's why you can find the 
cookie in request.cookies.)

So you need to take the session cookie and (like the browser) send it along 
with the urllib2 request. (I could be missing something, but I don't think you 
need basic auth here.)

> 
> Thanks
> Peter
> 
> On Dec 13, 7:27 pm, Anthony <abasta...@gmail.com> wrote:
>> On Tuesday, December 13, 2011 2:09:31 PM UTC-5, peter wrote:
>> 
>>> def test():
>>>         import urllib
>>>         url="http://127.0.0.1:8000/admin/default/site";
>>>         f = urllib.urlopen(url)
>>>         s = f.read()
>>>         return s
>> 
>>> I have to login again even though I am logged in on my computer. So
>>> the question is, can I somehow add the 'context' to s so that it
>>> recognises that I am already logged in?
>> 
>>> I am using web2py's admin as an example, but the solution should work
>>> for any website.
>> 
>> Are you saying you want to programmatically login to a website and then
>> retrieve a particular page that requires a login on that site? Look into
>> urllib2:http://docs.python.org/howto/urllib2.html. I think it's pretty
>> easy if the site supports basic authentication, otherwise a little more
>> complex (requires posting form data and managing cookies).
>> 
>> Anthony


Reply via email to