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':"myem...@rutgers.edu", '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 web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/6cbfabe2-6017-4b46-acc7-7eb664634669n%40googlegroups.com.

Reply via email to