Python development _getpass_ to obtain the login name

Sometimes we need to get to the login name, the computer at this time, you can use the getpass module in Python.

Below is my demo

Effect of operation:

==================================================

Part of the code:

==================================================

 1 #python getpass
 2 
 3 #Author   :   Hongten
 4 #Mailto   :   
 5 #Blog     :    
 6 #QQ       :   648719819
 7 #Version  :   1.0
 8 
 9 import getpass
10 
11 '''
12     The getpass module provides two functions:
13 
14     getpass.getpass(prompt = 'Password:', stream = None)
15         Prompt the user form a password without echoing.
16         The user is prompted using the string prompt,which
17         defaults to 'Password:' .On Unix, the prompt is written
18         to the file-like object stream.stream defaults to the
19         controlling terminal(/dev/tty) or if that is unavailable
20         to sys.stderr(this argument is ignoed on Windows)
21 
22         if echo free input is unavailable getpass() falls back to
23         printing a warning message to stream and reading from
24         sys.stdin and issuing a GetPassWarning.
25 
26         Availablity:Macintosh,Unix,Windows.
27 
28         Note:
29         if you call getpass from within IDLE,the input may be done in
30         the terminal you launched IDLE from rather than the idle window
31         itself.
32 
33         exception getpass.GetPassWarning
34             A UserWarning subclass isssued when password input may be echoed.
35 
36         getpass.getuser()
37             Return the 'login name' of the user.Availavility:Unix,Windows.
38 
39             This function checks the environment variables LOGNAME, USER,LNAME and
40             USERNAME,in order, and returns the values of the first one which is set
41             to a non-empty string.If none are set,the login name from the password
42             database is returned on systems which support the pwd module,otherwise,
43             an exception is raised.
44 
45 '''
46 def get_system_user_name():
47     '''return the 'login name' of the user.'''
48     return getpass.getuser()
49 
50 def main():
51     user_name = get_system_user_name()
52     print('the system user\'s name is : [{}]'.format(user_name))
53 
54 if __name__ == '__main__':
55     main()

Posted by Alfred at November 17, 2013 - 2:56 PM