I'm currently trying to transfer some of my code to either urllib3 or requests. urllib3 would be the preferred method to reduce the modules imported.
This is the code currently working.
payload = ["<username>", "<password>"
browser = mechanize.Browser()
browser.open(url)
browser.select_form('form1')
browser['j_username'] = payload[0]
browser['j_password'] = payload[1]
browser.submit()
What I tried so far is
with requests.Session() as session:
#not working
payload = { "j_username" : "<username>", "j_password" : "<password>" }
req = session.post(url, data = payload)
print(req.url)
#also not working
req = session.get(url, auth=("<username>", "<password>") )
print(req.url)
Also not working:
payload = { "lang": "de", "j_username": "<username>", "j_password": "<password>", "_eventId_proceed": "" }
req = session.post(url, data = payload)
I'm always getting the same url returned that so I can't login.
Is there a way to do this login in urllib / requests or do I have to stick with mechanize? Also I would like to compare speed on those solutions.
Finally I found a solution, and maybe it will help someone else too. I had to do a get request and use the url from that request because there is a redirect to the login-page and you can't open it directly (I would see a page like You may be seeing this page because you used the Back button while browsing a secure web site or application...)
#url1 = url that redirects to loginpage
payload = { "lang": "de", "j_username": "<username>", "j_password": "<password>", "_eventId_proceed": "" }
with requests.Session() as session:
loginPage = session.get(url1)
afterLogin = session.post(loginPage.url, data = payload)
wantedPage = session.get(<url of wanted page>)