I have written some code for a system, I have imported another file which stores all the information for login details. But when I go to test it and try to login it keeps coming up with "INCORRECT". Both of the code files are attached.
I have tried changing the names of the files, variables and changing the login details but it still doesn't work.
from database import user_data, pasw_data, no_file
name = user_data
code = pasw_data
def user_check():
user = input("USERNAME >>")
if user == name:
pasw_check()
else:
print("INCORRECT")
def pasw_check():
pasw = input("PASSWORD >>")
if pasw == code:
print("ACCESS GRANTED")
user_check()
This is the file, which stores all the login info, named database.py
user_data = ["123"]
pasw_data = ["python"]
You're checking a string (user) and a list (user_data) for equality. They aren't equal at all. The list just happens to contain a string that's equal to your query. You should use in to search lists (and strings, dictionaries, tuples, etc) for data:
if user in user_data:
print("I'm in!")
Related
In summary I'm trying to create a password manager. The Idea is that the program would ask the user input.
If user writes "new", the program asks input on the website, username and password and then store this data in a text file in the form of a List.
Now the main problem:
I want to be able to access selected data and have the program print said data from the text file to me.
For example:
I input into the program the website "google" along with username: "potato" and password: "potato"
After that, the program asks me what else I want to do. And if I write "access google", I want to program to give me back the website, username and password that are SPECIFIC to the google input.
This is necessary, as I will be adding several different inputs.
I have no idea how to do this and have no tutor. I hope someone can give a solution I can learn from.
Below you will find the base code I have come up with.
Keep in mind that I am a beginner. Thank you.
vault = open("Passvault.txt", "r+")
list = []
action = input("What do you want to do? ")
def tit():
global title
title = input("Add website: ")
return title
def user():
global username
username = input("Create username: ")
return username
def passw():
global password
password = input("Create password: ")
return password
running = True
while running:
creation = True
tit()
user()
passw()
if action == "new":
tit()
user()
passw()
#I added a class here hoping that i could create a class with an argument referencing the title
#so that when i type access "title" in the next if statement it would print back the data
#relevant to the selected title
class new(str(title)):
list.append(tit)
list.append(user)
list.append(passw)
vault.close()
if action == "access" + title:
creation = False
print(title)
print("Username: " + username)
print("Password: " + password)
vault.close()
Here is the code. This code stores the username, password, and website name in the txt file and also prints the username and password w.r.t website name.
import re # used to search patterns.
file_name = 'Passvault.txt'
while True:
action = input("What do you want to do? ")
if action == 'new':
title = input("Add website: ")
username = input("Create username:")
password = input('Create password')
#writing data into the file
with open (file_name,'a') as f:
data = f.write(f'{title} {username} {password}\n')
if 'access' in action:#if input contains access word
website = action.split()[1].strip() #storing website name which is written after access
#reading the data
with open(file_name,'r') as f:
data = f.read()
#searching for all username password related to website
username_pass = re.findall(f'{website}\s+(.*?)\n',data,re.S)#example google sachin 1234
print('Website: ',website)
print('Username, Password',username_pass)
I'm going to give you a more conceptual answer than code, because this is a more long term type of deal.
What you're going to want to do is use json files and dictionaries to store your data so you can search by keys (see the dictionaries link).
Once you've done that you're going to want to wrap everything in a while loop inside a def that gets user input like so:
def get_input():
permitted_actions = ['new', 'access', 'exit']
while True:
action = input("What do you want to do? Valid actions: new, access or type EXIT to end the program.").strip().lower()
if action not in permitted_actions:
print(f"{action} is not a valid action!")
elif action == 'new':
#call another function to do stuff here
elif action == 'access':
#call another function to do stuff here
elif action == 'exit':
print("Shutting down...")
break
I would highly recommend against creating your own actual vault for a password manager until you're much more experienced if you actually intend to use this, otherwise feed it fake passwords and whatnot and learn.
Now when you're adding website data you'll read your dictionary (see the dictionary link) and get the key associated with said website if it exists and then update the info that the user gives.
When you're accessing a website's data you just go to the dictionary (see the dictionary link) and grab the info relating to that website key if it exists.
Remember, you're going to be loading that dictionary from a json file (see the json link).
If you were to make this program an actual program someone would use you'd use a proper database of some sort (python3 has sqlite support natively) and use a database with encryption and master passwords.
I hope this points you in the right direction.
You can save the values as a dictionary with a list as the username and password then use literal_eval to convert the string dict into a dict and access the username and password as well as storing other websites with its own usernames and passwords.
website = 'google'
username,password = 'potato', 'potato'
filename = "yourfilehere"
with open(filename, w) as f:
f.write(str({website: [username,password]}))
#This will save your data as a string dictionary will the data above
#then read the file, get the dictionary and convert it then use its values
from ast import literal_eval
with open(filename, 'r') as f:
data = f.read()
data = literal_eval(data)
#Then search dictionary for website
found = data.get(website)
#Then if it was successful get the username and password
username = found[0]
password = found[1]
As long as you only have the dictionary created as a string in the file you are reading you can use this ethod to save as many website with the allocated username and password saved with it.
You can add a input() into the code to check which site the user wants the username and password for and then search for it in your dictionary.
search = input("Enter the website: ")
try:
found = data.get(search)
#add code here to get username and password
except:
print("failed to find a website matching: %s" % search")
Hi there I'm trying to add checks to my embed definitions that return the users information from the sql database.
What I'd like to achieve is to check if the user has set a gametag and then if the data isn't there then don't show it on their profile.
However, I'm was able to pass a check to see if the user data is in the database if the result is None it will turn Steam None where as I rather not show it altogether.
Here is what I'm working with:
#commands.group(invoke_without_command=True)
async def profile(self, ctx, user: discord.Member=None):
user = user or ctx.author
db = sqlite3.connect('profiles.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT profile FROM profile WHERE username={user.id}")
result = cursor.fetchone()
cursor.execute(f"SELECT steam FROM profile WHERE username={user.id}")
result2 = cursor.fetchone()
if result is None:
await ctx.send(f"{user.display_name}'s bio has not been created yet.")
elif result4:
steam = f"**Steam** [{result2[0]}](http://steam.com/{result2[0]})" #return data from database.
else:
steam = "" # return nothing if nothing returned from database.
desc = f"{(result[0])} \n\n {steam}:" # define a embed description as f-string
embed = discord.Embed(title=f"{user.name}'s Profile", description=desc, color=user.colour)
await ctx.send(embed=embed)```
If I understand correctly, you do not want result2[0] to be None, whereas you are checking for result to be None. Make sure to check for both result2 and result2[0] to be not None and that should fix it.
Also, if that is supposed to stop the embed creation, you might want to return after the await ctx.send(...) (under "if result == None:").
I'm working on an email list program for my band, and although I found out how to append to the list of fans using user input, when I restart the program the information doesn't stay in there. I want to be able to turn the program into an executable application where you can input information and it permanently saves. Just so I'm not giving out emails and names and locations, I created an example and then after the example I will put an Imgur link to what the console looks like if I search All Data while the loop is still running and what it looks like once I restart the program and try again(it doesn't show up when I restart):
class Emails:
def __init__(self, name, location, email):
self.name = name
self.location = location
self.email = email
fans = [Emails('james franco', 'california', 'jamesfranco#gmail.com'),
Emails('john cena', 'california', 'johncena#hotmail.com')
]
def append_input():
input_name = input('Add Name: ')
input_location = input('Add Location: ')
input_email = input('Add Email: ')
fans.append(Emails(input_name, input_location, input_email))
def all_data():
for fan in fans:
print(fan.name + '\n')
print(fan.location + '\n')
print(fan.email + '\n________________')
while True:
start_search = input("What would you like to search?(Name/Location/Email/All Data/Add Data): ")
if start_search == "Add Data":
append_input()
if start_search == "All Data":
all_data()
Before refreshing console and inputing "all data" to check(works fine)
After refreshing console and inputing "all data" to check. Lil wayne is not there.
You guys have saved me many times, but if you save me with this, then my entire program is basically finished. Thank you for your kindness and the time you take to answer all of my questions friends!
In order for data to be saved and persist across multiple runs, you have to actually save it to file on disk or database. If you don't do it, it only stays in memory for the duration of the program and getting wiped when program exits.
Persistent storage can be a simple CSV file that you load when program starts up and save entries there as users input. However, DB (such as SQLite) might be better as you can index data, avoid duplicate entries, query it, etc.
Python has modules for both CSV and SQLite and other DBs. Here is SQLite tutorial:
https://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html
How do I fix the variable assignment in this Python code? So, I have this python code:
with open('save.data') as fp:
save_data = [line.split(' = ') for line in fp.read().splitlines()]
with open('brute.txt') as fp:
brute = fp.read().splitlines()
for username, password in save_data:
if username in brute:
break
else:
print("didn't find the username")
Okay so, a quick explanation; thesave.data is a file that contains variables of Batch-file game (such as username, hp etc...) and brute.txt is a file that contains "random" strings (like what seen in wordlists used for brute-force).
save.data:
username1 = PlayerName
password1 = PlayerPass
hp = 100
As I said before, it's a Batch-file game so, no need to quote strings.
brute.txt:
username
username1
password
password1
health
hp
So, when the Python code is executed, it loads the two files contents and save them into a list and then iterate through the username and password "brute" them until they match with what on brute.txt, and they assign themselves automatically. But, the problem is with the assignment, when i try to print them (the variables) this happens:
## We did all the previous code
...
>>> print(save_data)
[['username', 'PlayerName'], ['password', 'PlayerPass'], ['health', '100']]
>>> print("Your username is: " + username)
username
>> print("Your password is: " + password)
PlayerName
>> print("Your health is: " + hp)
NameError: name 'hp' is not defined
So, any idea on how to fix the assignment conflict? If you didn't understand something, kindly comment it and I will clear it up.
and they assign themselves automatically
This isn't a thing. I suppose you're imagining that the pseudo-variables pseudo-defined in save.data will become Python variables in your program. They won't.
Instead, parse them into a data structure and retrieve the values from the data structure.
For example,
with open('save.data') as fp:
save_data = dict([line.split(' = ') for line in fp.read().splitlines()])
...
print(save_data["hp"])
so before i start AS-level computer science next year, i have a little practice task to do over the summer.
Its a game that requires a log in with username and password, and then requires to store the users scores.
Originally i was just going to have a .txt file named after each user, with the first line being a plaintext password, which the program reads during login. to save the score i was going to append the raw score onto a new line at the end of the document.
Now i think about it, im wondering if it would be better and tidier to have a .csv file and save all the usernames of each user in the first column, then a salted and hashed password on the second column, relating to the user by being in the same row (mainly because i think thats interesting and would like to learn it, rather that it being necessary for my program) then having the next rows each score taken.
I don't really know how i would go about checking the first column to find if there is a username the same as the user is trying to input, then how i would compare the password inputted to the password in same row but one column along
Any advice will be greatly appreciated, however in the meantime i will try and figure it out (afterall you should "Code to Learn, not Learn to Code")
i will post back if i get anywhere
thanks in advance for any help :)
Alex
I don't think csv is the right format to store passwords in Python. How about using json?
I've written some example which wraps around some dict, and stores it's content into a json-file:
import codecs
import crypt
import hmac
import json
import os
class Secret:
def __init__(self, filename='passwords.json'):
self.filename = filename
self.content = self.read_file()
def read_file(self):
if os.path.exists(self.filename):
with codecs.open(self.filename, 'r', encoding='utf-8') as rf:
return json.loads(rf.read())
# create defaults if file does not exist
return dict(
salt=crypt.mksalt(),
users=dict()
)
def write_file(self):
with codecs.open(self.filename, 'w', encoding='utf-8') as wf:
return wf.write(json.dumps(self.content, sort_keys=True, indent=4))
def set_user_password(self, name, password):
self.content['users'][name] = crypt.crypt(name, password)
self.write_file()
def check_user_password(self, name, password):
if name in self.content['users']:
hashed = self.content['users'][name]
if hmac.compare_digest(hashed, crypt.crypt(name, password)):
return True
return False
Please note:
This Example is blindly based on the documentation of the crypt-module - I have no clue if this in any way secure (probably not).
Further: I don't know if it's good to store the salt alongside the passwords.
You can use it like this:
if __name__ == '__main__':
secret = Secret(filename='passwords.json')
secret.set_user_password('demo_user', 'lolcat')
for user, password in [
('demo_user', 'lolcat'),
('demo_user', 'wrong_pass'),
('wrong_user', 'wrong_pass'),
]:
print(
'user:', user, 'password:', password,
'\n-->', secret.check_user_password(user, password)
)
It creates a json-File like this:
{
"salt": "rT",
"users": {
"demo_user": "lo1JY.PCooh4."
}
}