Skip to content
Advertisement

check if login was correct with python requests

I’ve been working on a program that would let you sign in to different web-pages and tells you if the sign in has been successfull. It Works by 1.Login into an account you know exists and is correct and copying the response text to a file, 2.Loging into an account that you know doesn’t work and doing the same thing. Then, when you try to login to that web page the program Will compare the responses to the previously saved responses and finds similarities. If it finds more similarity’s with the correct login text, this means it succeeded, if not it didn’t. But this doesn’t seem to work and I can’t figure out why. I think it is because of the mechanism used to compare the responses, but I’m not sure. I’ll leave the code here. Thanks for any answers!

import requests

def checkStatus(response):     #we check if the response let us login by comparing it to previous responses. This algorythm should be able to work for every webpage
    check1 = 0
    check2 = 0
    
    file1 = open("wronglogin.txt", "r")
    file2 = open("correctlogin.txt", "r")

    i = 0
    
    for line in response:
        try:
            if file2.read(i) == line:
                check2 += 1
            i+=1
        except:
            break

    i = 0

    for line in response:
        try:
            if file1.read(i) == line:
                check1 += 1
            i+=1
        except:
            break
    print(check1)
    print(check2)
    if check1 < check2:
        return True
    else:
        return False

url = "https://twitter.com/explore"
username = "your_username"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}

password = "your_password"
form_data = { "username":str(username), "password":str(password) }

s = requests.Session()

response = s.post(url, headers = headers, data = form_data)

print(response.text)

if checkStatus(response.text) == True:
    print(form_data["password"])

Advertisement

Answer

After checking the code a bit more I found out that it isn’t comparing the content of the response i get when posting line by line, but digit by digit, but it is Reading the content of the files as a line. Of course this means that it won’t work, so I would recommend to save the content of the response you want to check in a file first, so that it is read line by line. However, the information you’re searching for, aka. the information wether you were logged in correctly or not is probably not in the content, but rather in the headers, the cookies, etc. To check all of these factors I’ve found a Little trick. Just like with an array you can actually make a for loop which checks all of the variables it may have. This would look something like this:

import requests

url = "https://twitter.com/explore"
username = "your_username"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36      (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}
password = "your_password"
form_data = { "username":str(username), "password":str(password) }
Session = requests.session()
response = Session.post(url, data=data, headers=headers)
for var in response:
    print(var)

var would be the data stored in the response. I’m not 100% sure if it is all the data though :/

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement