Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
**** PassWord
#1
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
Reply
#2
Please post the code you've written so far and any error codes.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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]
Reply
#4
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).
Reply
#5
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
Reply
#6
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
Reply
#7
(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.
Reply
#8
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!
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(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 :)
Recommended Tutorials:
Reply
#10
Yes but I don't use Python 2 and no one should.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020