I want to make a password script in python that changes my input to *,
so when i type 1234 you can only see ****
pa=getpass.getpass("Password: "), i can't see how many characters i typed
pa=raw_input("Password: ") , i can see exactly what i typed
Please post the code you've written so far and any error codes.
You might want to look at getpass. It doesn't print out
anything at all
[python]
import getpass
zz = getpass.getpass('Please enter your well thought out password: ')
[/password]
OP mentioned that he already tried both
raw_input()
and
getpass.getpass()
and neither one does what he wants - to echo passwords with "*' while typing. Little googling reveals that it is not so easy (platform dependent) and
How to read password with echo “*” in Python console program? shows solution by modifiing getpass implementation (windows only).
getpass is a module in the standard lib, it is 100% python code. Here's the source:
https://github.com/python/cpython/blob/3...getpass.py
It uses getch, which gets a single character from input instead of a whole line. In order to do so without also echoing what you type, it uses a different method depending on your operating system. So, I copied a lot of that, and changed it a bit to ALSO echo out an asterisk while waiting for input.
import sys
def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch
# POSIX system. Create and return a getch that manipulates the tty.
import tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch
getch = _find_getch()
pword = ''
ch = 'spam'
# keep looping until "enter" is hit
while ch not in '\r\n':
ch = getch().decode()
pword += ch
print('*', end='')
# we need to flush, or else the asterisks won't appear
# until after the password is finished getting entered
sys.stdout.flush()
# add a newline after the asterisks
print()
# prove that we actually got something
print(pword)
Output:
D:\Projects\playground>python pword.py
*********************************
spam, spam, spam, spam, and eggs
import time
import os
ans = True
while ans:
print("""
----------
1. EXIT
2. START
----------
""")
ans=raw_input("""
Kies een nummer: """)
if ans == "1":
exit()
elif ans == "2":
pa=raw_input(" Wachtwoord: ")
if pa == "Tom Zeef":
print("""
Welkom Tom
---------------
1. Pi password
2. Exit
---------------
""")
pe=raw_input ("""
Kies een nummer: """)
if pe == "1":
print ("""
Pi's Password Is Adminofpi""")
import time
time.sleep(1)
exit()
elif pe == "2":
exit()
else:
print("""
Fout Antwoord. ERROR""")
import time
time.sleep(1)
exit()
else:
print("""
Fout Wachtwoord. Jij zal GESTRAFT worden!!!""")
import time
time.sleep(1)
#os.startapp('ERROR.app/')
exit()
Its in Dutch
(Mar-22-2017, 01:54 PM)kadoushka Wrote: [ -> ]import time
import os
ans = True
while ans:
print("""
----------
1. EXIT
2. START
----------
""")
ans=raw_input("""
Kies een nummer: """)
if ans == "1":
exit()
elif ans == "2":
pa=raw_input(" Wachtwoord: ")
if pa == "Tom Zeef":
print("""
Welkom Tom
---------------
1. Pi password
2. Exit
---------------
""")
pe=raw_input ("""
Kies een nummer: """)
if pe == "1":
print ("""
Pi's Password Is Adminofpi""")
import time
time.sleep(1)
exit()
elif pe == "2":
exit()
else:
print("""
Fout Antwoord. ERROR""")
import time
time.sleep(1)
exit()
else:
print("""
Fout Wachtwoord. Jij zal GESTRAFT worden!!!""")
import time
time.sleep(1)
#os.startapp('ERROR.app/')
exit()
Its in Dutch
(Mar-22-2017, 01:54 PM)kadoushka Wrote: [ -> ]Its in Dutch
It also...
1) isn't in code tags,
2) doesn't do what the OP was looking for.
from prompt_toolkit import prompt
import getpass
name = prompt("Username: ")
passwd = prompt("Password: ", is_password=True)
if name.lower() != 'Agent Smith' and passwd != 'scaryface':
print("\nValidation failed! Exterminate! Exterminate!")
Output:
Username: Agent Smith
Password: ******************
Validation failed! Exterminate! Exterminate!
(Apr-03-2017, 02:13 AM)wavic Wrote: [ -> ]from prompt_toolkit import prompt
import getpass
name = prompt("Username: ")
passwd = prompt("Password: ", is_password=True)
if name.lower() != 'Agent Smith' and passwd != 'scaryface':
print("\nValidation failed! Exterminate! Exterminate!")
Output:
Username: Agent Smith
Password: ******************
Validation failed! Exterminate! Exterminate!
Note if using python2.x you also have to
from __future__ import unicode_literals
I like that lib, never heard of it before :)
Yes but I don't use Python 2 and no one should.