Running python on server, executing commands from computer - python-3.x

I made a login system with python. It works perfectly, but i want to run script on server or web. For example: Steam. Steam wants username and password to log in. So i wanted to do the same for my script. How can i do that?
My Code:
import os
import string
import time
version = "1.0 Alfa"
def login():
print ("----------------------------------------")
print (" Login ")
print ("----------------------------------------")
k_name = input("Enter username: ")
if os.path.exists(k_name + ".txt") == False:
print ("Username not found.")
create()
else:
k_pass = input("Enter password: ")
with open(k_name + ".txt", "r") as f:
if k_pass == f.read():
print("Welcome %s!"%k_name)
f.close()
input()
else:
print("Password is wrong!")
create()
def create():
print("You using login system %s" % version)
print( "----------------------------------------")
print("| Lobby |")
print( "----------------------------------------")
starting = input("To create user type R, to login type L").upper()
if starting == "R":
name = input("Enter username: ")
password = input("Enter password: ")
password2 = input("Enter password again: ")
if password == password2:
newfile = open(name + ".txt", "w")
newfile.write(password)
newfile.close()
print("User created. Redirecting you to login.")
time.sleep(2)
login()
elif password != password2:
print("Passwords doesn't match.")
input()
create()
elif starting == "L":
login()
else:
print("\nWrong button\n")
create()
create()

Here is the script which you can run : python test.py user pass
it will save data if file not found and perform login
#!/usr/bin/env python
import sys, getopt
import os
import time
version = "1.0 Alfa"
def login(username=None, password=None):
print ("----------------------------------------")
print (" Login ")
print ("----------------------------------------")
if username:
k_name = username
else:
k_name = input("Enter username: ")
if os.path.exists(k_name + ".txt") == False:
print ("Username not found.")
create(username, password, "R")
else:
if password:
k_pass = password
else:
k_pass = input("Enter password: ")
with open(k_name + ".txt", "r") as f:
if k_pass == f.read():
if not username:
print("Welcome %s!"%k_name)
f.close()
input()
else:
print("Password is wrong!")
create()
def create(username=None, password=None, mode="L"):
print("You using login system %s" % version)
print( "----------------------------------------")
print("| Lobby |")
print( "----------------------------------------")
if mode:
starting = mode
else:
starting = input("To create user type R, to login type L").upper()
if starting == "R":
if username:
name = username
else:
name = input("Enter username: ")
if password:
password2 = password
else:
password = input("Enter password: ")
password2 = input("Enter password again: ")
if password == password2:
newfile = open(name + ".txt", "w")
newfile.write(password)
newfile.close()
print("User created. Redirecting you to login.")
time.sleep(2)
login(username, password)
elif password != password2:
print("Passwords doesn't match.")
input()
create()
elif starting == "L":
login(username, password)
else:
print("\nWrong button\n")
create()
def main(argv):
print sys.argv
if len(sys.argv) < 3:
print 'test.py <username> <password>'
sys.exit()
username = sys.argv[1]
password = sys.argv[2]
print 'username is ', username
print 'password is ', password
create(username, password)
if __name__ == "__main__":
main(sys.argv[1:])

Related

Why isn't my function save_users(): working? How do I save these passwords to file?

from hashlib import sha256
import random, sys
def hash(string):
'''Hashes a string'''
return sha256(
string.encode()).hexdigest() # when using hash() returns a hashed string
def save_users():
f = open("UserDetails.txt", "w")
f.write("Username;{} \nPassword:{}".format(username, password))
f.close
print("\nWelcome")
signIn = input("Do you have an account? [Y/N]").upper()
if signIn == "N":
print("Sign up :")
username = input("New Username: ")
password = hash(input("New Password: "))
confirm = hash(input("ConfirmPassword: ")) == password
print(password)
if not confirm:
print("Passwords do not match")
save_users()
"print (password)" is just there for me to test if it actually hashed the string as this is my first time doing anything like this. How would I save the password and username to an email, and similarly how would I authenticate the password and username?
Python 3.8.5
Replace:
def save_users():
f = open("UserDetails.txt", "w")
f.write("Username;{} \nPassword:{}".format(username, password))
f.close
With:
def save_users(username, password):
f = open("UserDetails.txt", "w")
f.write("Username;{} \nPassword:{}".format(username, password))
f.close()
And call it with save_users(username, password) instead of save_users()
from hashlib import sha256
def hash(string):
'''Hashes a string'''
return sha256(
string.encode()).hexdigest()
def save_users(username, password):
with open("UserDetails.txt", "w") as f:
f.write("Username;{} \nPassword:{}".format(username, password))
print("\nWelcome")
signIn = input("Do you have an account? [Y/N]").upper()
if(signIn == "N"):
print("Sign up :")
username = input("New Username: ")
password = hash(input("New Password: "))
confirm = hash(input("ConfirmPassword: ")) == password
if(confirm):
save_users(username, password)
else:
print("Passwords do not match")
else:
print("you are already have an account")
Test this

Python3 with Loop function / password exit?

I'm new to Python and need a little help. I came across this code on here which I kind of understand and want to expand on it... but I don't know how to get out of the loop!
When you run the code and enter the specified username and password, it runs the defined function logged()... but then loops back to asking for the username again because it run the main() function again!... how can I get past this. When the correct username & password is entered, I would like to be at a point where I can add new code! Does this make sense?
import os
import time
#Must Access this to continue.
def main():
while True:
UserName = input ("Enter Username: ")
PassWord = input ("Enter Password: ")
if UserName == 'Bob' and PassWord == 'rainbow123':
time.sleep(1)
print ("Login successful!")
logged()
else:
print ("Password did not match!")
def logged():
time.sleep(1)
print ("Welcome to ----")
main()
When all matches, break out of loop and call logged outside the loop. Or else once the logged is over, you return back to the infinite loop again.
def main():
while True:
UserName = input ("Enter Username: ")
PassWord = input ("Enter Password: ")
if UserName == 'Bob' and PassWord == 'rainbow123':
time.sleep(1)
print ("Login successful!")
break
else:
print ("Password did not match!")
logged()

Why does my program print "invalid Username or Password" when it's correct

I have tried many different imputs but they all say print "Invalid". I would appreciate if anyone can explain or edit my code so i can understand.
username = []
password = []
def account_creation():
account = input("Would you like to make an Account? Yes or No: ")
if account == "Yes" or "yes":
username = input("Please make a Username: ")
password = input("Please make a Password: ")
else:
return
account_creation()
def login():
account = input("Do you have an Account? Yes or No: ")
if account == "Yes":
login_username = input("Plese enter your username: ")
login_password = input("Plese enter your password: ")
else:
return
if login_username and login_password == username and password:
print("Welcome back")
else:
print("Invalid Username or Password")
login()
you need to first check you username-password check line (as _venky stated):
...
if login_username == username and login_password == password:
...
but you have something more important missing here.
you are trying to change global variables inside a function, thus you need global keyword with related variables in the function body.
def account_creation():
global username,password
account = ... the rest of the code

Python code [Code loops]

Hello am very new to python and ive atempted my first code like this but something seems to be wrong and one of the steps keeps looping. I am very confused on what to do so could someone please help me out.
Thank you!
import os
import time
def main():
while True:
print("Welcome To Amazon")
search = input("Search.... ")
if 'search == registration':
reg()
if 'search == login':
login()
#Must Register to continue
def reg():
while True:
print("Display Name")
reg_user = input()
print("Password")
reg_pass = input()
def registered():
time.sleep(1)
print("Registration Successful!")
main()
#Must Login to continue
def login():
while True:
print("Enter Username: ")
username = input()
print("Enter Password: ")
password = input()
if 'username == reg_user' and 'password == reg_pass':
time.sleep(1)
print("Login Successful!")
logged()
else:
print("Try Again!")
def logged():
time.sleep(1)
print("Welcome To CityRP Gaming")
main()
The while loop loops for as long as the condition is true. You used While True, and True will always be True. This means the loop will continue forever. To break out of a loop you can use 'break'.

how to check if the user exist in database list

After so many hours I still can't figure out how to check if the name and password user input exist in my data. For example, when I ask Please input customer name: and they input Sam than I ask again Please input customer password: and the input is janos i want customer_menu() function to be called. thanks
customers_list = []
class BankSystem(object):
def __init__(self):
self.customers_list = []
self.load_bank_data()
def load_bank_data(self):
customer_1 = Customer("Sam", "janos", ["14", "Wilcot Street", "Bath", "B5 5RT"])
account_no = 1234
account_1 = Account(5000.00, account_no)
customer_1.open_account(account_1)
self.customers_list.append(customer_1)
def customer_login(self, name, password):
if name in customers_list and password in customers_list:
self.name = name
self.password = password
self.customer_menu()
else:
print("sorry %s, it doesn't look like you are a customer"%name)
exit()
def main_menu(self):
print ("1) Customer login")
print (" ")
option = int(input ("Choose your option: "))
return option
def run_main_option(self):
loop = 1
while loop == 1:
choice = self.main_menu()
if choice == 1:
name = input ("\nPlease input customer name: ")
password = input ("\nPlease input customer password: ")
msg = self.customer_login(name, password)
print(msg)
person.py
class Person(object):
def __init__(self, name, password, address = [None, None, None, None]):
self.name = name
self.password = password
self.address = address
def get_address(self):
return self.address
def update_name(self, name):
self.name = name
def get_name(self):
return self.name
def print_details(self):
print("Name %s:" %self.name)
print("Address: %s" %self.address[0])
print(" %s" %self.address[1])
print(" %s" %self.address[2])
print(" %s" %self.address[3])
print(" ")
def check_password(self, password):
if self.password == password:
return True
return False
def profile_settings_menu(self):
#print the options you have
print (" ")
print ("Your Profile Settings Options Are:")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print ("1) Update name")
print ("2) Print details")
print ("3) Back")
print (" ")
option = int(input ("Choose your option: "))
return option
def run_profile_options(self):
loop = 1
while loop == 1:
choice = self.profile_settings_menu()
if choice == 1:
name=input("\n Please enter new name\n: ")
self.update_name(name)
elif choice == 2:
self.print_details()
elif choice == 3:
loop = 0
customer.py
from person import Person
class Customer(Person):
def __init__(self, name, password, address = [None, None, None, None]):
super().__init__(name, password, address)
def open_account(self, account):
self.account = account
def get_account(self):
return self.account
def print_details(self):
super().print_details()
bal = self.account.get_balance()
print('Account balance: %.2f' %bal)
print(" ")
for customer in customers_list:
if customer.name == name:
if customer.password == password:
...

Resources