getpass.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """Utilities to get a password and/or the current user name.
  2. getpass(prompt) - prompt for a password, with echo turned off
  3. getuser() - get the user name from the environment or password database
  4. On Windows, the msvcrt module will be used.
  5. On the Mac EasyDialogs.AskPassword is used, if available.
  6. """
  7. # Authors: Piers Lauder (original)
  8. # Guido van Rossum (Windows support and cleanup)
  9. import sys
  10. __all__ = ["getpass","getuser"]
  11. def unix_getpass(prompt='Password: '):
  12. """Prompt for a password, with echo turned off.
  13. Restore terminal settings at end.
  14. """
  15. try:
  16. fd = sys.stdin.fileno()
  17. except:
  18. return default_getpass(prompt)
  19. old = termios.tcgetattr(fd) # a copy to save
  20. new = old[:]
  21. new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
  22. try:
  23. termios.tcsetattr(fd, termios.TCSADRAIN, new)
  24. passwd = _raw_input(prompt)
  25. finally:
  26. termios.tcsetattr(fd, termios.TCSADRAIN, old)
  27. sys.stdout.write('\n')
  28. return passwd
  29. def win_getpass(prompt='Password: '):
  30. """Prompt for password with echo off, using Windows getch()."""
  31. if sys.stdin is not sys.__stdin__:
  32. return default_getpass(prompt)
  33. import msvcrt
  34. for c in prompt:
  35. msvcrt.putch(c)
  36. pw = ""
  37. while 1:
  38. c = msvcrt.getch()
  39. if c == '\r' or c == '\n':
  40. break
  41. if c == '\003':
  42. raise KeyboardInterrupt
  43. if c == '\b':
  44. pw = pw[:-1]
  45. else:
  46. pw = pw + c
  47. msvcrt.putch('\r')
  48. msvcrt.putch('\n')
  49. return pw
  50. def default_getpass(prompt='Password: '):
  51. print "Warning: Problem with getpass. Passwords may be echoed."
  52. return _raw_input(prompt)
  53. def _raw_input(prompt=""):
  54. # A raw_input() replacement that doesn't save the string in the
  55. # GNU readline history.
  56. prompt = str(prompt)
  57. if prompt:
  58. sys.stdout.write(prompt)
  59. line = sys.stdin.readline()
  60. if not line:
  61. raise EOFError
  62. if line[-1] == '\n':
  63. line = line[:-1]
  64. return line
  65. def getuser():
  66. """Get the username from the environment or password database.
  67. First try various environment variables, then the password
  68. database. This works on Windows as long as USERNAME is set.
  69. """
  70. import os
  71. for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
  72. user = os.environ.get(name)
  73. if user:
  74. return user
  75. # If this fails, the exception will "explain" why
  76. import pwd
  77. return pwd.getpwuid(os.getuid())[0]
  78. # Bind the name getpass to the appropriate function
  79. try:
  80. import termios
  81. # it's possible there is an incompatible termios from the
  82. # McMillan Installer, make sure we have a UNIX-compatible termios
  83. termios.tcgetattr, termios.tcsetattr
  84. except (ImportError, AttributeError):
  85. try:
  86. import msvcrt
  87. except ImportError:
  88. try:
  89. from EasyDialogs import AskPassword
  90. except ImportError:
  91. getpass = default_getpass
  92. else:
  93. getpass = AskPassword
  94. else:
  95. getpass = win_getpass
  96. else:
  97. getpass = unix_getpass