'Python Requests: Check if Login was successful

I've looked all over the place for the solution I'm looking for but just can't find it. Basically, I'm developing a tool which takes a list of URLs from a text document, logs into them with your username/password, and returns which ones work.

I have the login figured out and all, but how do I actually return if the login works? Sites entered on the list won't necessarily use cookies and will be developed on various platforms.

Here's my login code:

r = requests.post(action, data=values, verify=False)
print(r.headers)
print(r.status_code)
print(r.content)
print(r.url)
sleep(1)

I'd like everything after that first line to be in an if statement if the login actually works, but I can't figure out how to actually determine that.

Thanks.



Solution 1:[1]

You can parse the r.content to check if you login or not, like string success or some flag means that you login. if the r.content for login user and not login user are same, you can request a special link that need to login and find a flag to check.

Solution 2:[2]

status bit will let you know

 r = requests.post(action, data=values, verify=False)
 authi = requests.get(r.url, auth=('user', 'pass'))

>>> authi.status_code
200

You will find more status bit info here

STATUS CODE DEFINITION

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daoctor
Solution 2