I found my error. on the lines where I have "response = requests.get(..."
and "response = requests.post(...", I should have "response =
session.get(..." and "response = session.post(..." so that web2py can set
and get the cookie data and the site headers through the session. I am
able to login now under python and it is working great. thank you again in
advance and I love web2py and python. Lucas
On Friday, March 26, 2021 at 2:54:04 AM UTC-4 lucas wrote:
> I'm trying to login to my own web2py website using requests in python
> 3.9.2. I can't seem to do it. my code is below. may you please review
> and let me know what I'm overlooking? thank you in advance, Lucas
>
> cookie = MozillaCookieJar('cookies')
> if os.path.exists('cookies'):
> cookie.load(ignore_discard=True, ignore_expires=True)
> session = requests.Session()
> session.cookies = cookie
> response = requests.get('
> https://website.org/application/default/user/login')
> BS = BeautifulSoup(response.text, 'lxml')
> data = { 'email':"[email protected]", 'password':"password",
> 'remember_me':"on" }
> for i, e in enumerate(BS.select('input[name]')):
> print(i, e)
> if (e['name'][0] == '_'):
> data[e['name']] = e['value']
> import random, string
> from requests_toolbelt import MultipartEncoder
> boundary = '----WebKitFormBoundary' +
> ''.join(random.sample(string.ascii_letters + string.digits, 16))
> m = MultipartEncoder(fields=data, boundary=boundary)
> print(data)
> headers = {
> 'Referer': "https://website.org/application/default/user/login",
> 'Origin': "https://website.org",
> 'Accept':
> "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
> 'Accept-Language': 'en-US,en;q=0.5',
> 'Accept-Encoding': 'gzip, deflate',
> 'Connection': 'keep-alive',
> 'User-Agent': user_agent,
> 'Content-Type': m.content_type
> }
> print(m)
> print(headers)
> response = requests.post('
> https://website.org/application/default/user/login#', data=m,
> headers=headers)
> print(response.url, response.history)
> print()
> print(response.status_code, response.encoding, response.headers, "...\n",
> response.text)
> cookie.save()
>
>
--
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 the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/168f7b33-fa06-43f2-9fa7-675839dc336en%40googlegroups.com.