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