Login to web2py from external script

99 views
Skip to first unread message

Richard Penman

unread,
Jan 11, 2015, 2:32:31 AM1/11/15
to web...@googlegroups.com
Do you happen to have made a script that can login to web2py?
It looks quite complex because have to manage the _formkey and login form uses multipart/form-data instead of application/x-www-form-urlencoded.

I have this working solution using mechanize:

br = mechanize.Browser()
b
r.open(login_url)
br
.select_form(nr=0)
br
['email'] = email
br
['password'] = password
response
= br.submit()


But looking for something using urllib2, and not loading a web2py specific library.


Richard Penman

unread,
Jan 11, 2015, 9:36:12 PM1/11/15
to web...@googlegroups.com
Strangely I found that the login forms works even when encode the data as usual for application/x-www-form-urlencoded:

cj = cookielib.CookieJar()
opener
= urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
html
= opener.open(login_url).read()

formkey
= xpath.get(html, '//input[@name="_formkey"]/@value')
data
= {'email': email, 'password': password, '_formkey': formkey, '_formname': 'login', '_next': '/', 'remember': 'on'}
encoded_data
= urllib.urlencode(data)
request
= urllib2.Request(url, encoded_data)
response
= opener.open(request)


Is that as expected?

Niphlod

unread,
Jan 12, 2015, 2:48:39 PM1/12/15
to
why shouldn't it be ? it's just different encodings to send the same data over the wire.

Richard Baron Penman

unread,
Jan 13, 2015, 12:12:27 AM1/13/15
to web...@googlegroups.com

Yeah same data of course, but different encoding than form specifies so I expected back end to reject it.

why it shouldn't be ? it's just different encodings to send the same data over the wire.


On Monday, January 12, 2015 at 3:36:12 AM UTC+1, Richard Penman wrote:

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/9ipjRoSnQvY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Jan 13, 2015, 5:04:07 AM1/13/15
to web...@googlegroups.com
http is stateless. 

Richard Baron Penman

unread,
Jan 13, 2015, 10:30:54 AM1/13/15
to web...@googlegroups.com
and web2py is not

Jim S

unread,
Jan 13, 2015, 10:55:43 AM1/13/15
to web...@googlegroups.com
Is there a 'how-to' anywhere that shows how to do this?  I'd really like to see this as a topic in Chapter 14: Other Recipes in the book.  Is it somewhere else where I've missed it?

-Jim

Richard Baron Penman

unread,
Jan 13, 2015, 7:27:59 PM1/13/15
to web...@googlegroups.com
> http is stateless

do you mean because the decoding is handled at the server layer before
reaches web2py?

On Tue, Jan 13, 2015 at 6:04 PM, Niphlod <nip...@gmail.com> wrote:

Niphlod

unread,
Jan 14, 2015, 3:25:53 AM1/14/15
to web...@googlegroups.com


On Wednesday, January 14, 2015 at 1:27:59 AM UTC+1, Richard Penman wrote:
> http is stateless

do you mean because the decoding is handled at the server layer before
reaches web2py?


I mean that web2py doesn't store anywhere how to send the data in. As long as it's valid, it's parsed. 
Reply all
Reply to author
Forward
0 new messages