1 00:00:00,000 --> 00:00:02,200 [no audio] 2 00:00:02,200 --> 00:00:05,800 Friends, here we are going to discuss about 'getpass' module. 3 00:00:06,500 --> 00:00:10,600 See this 'getpass' module consists of two important functions, 4 00:00:10,600 --> 00:00:12,700 they are 'getpass()' and 'getuser()'. 5 00:00:13,500 --> 00:00:16,200 See you know that if you want to see what are the different 6 00:00:16,200 --> 00:00:18,600 functions are there with your module what you have to do? 7 00:00:18,600 --> 00:00:22,900 Simply first enter into your Python, then try to import that 8 00:00:22,900 --> 00:00:27,800 module, 'getpass'. But I am giving some wrong module name 9 00:00:27,800 --> 00:00:31,000 'getpasspass', something. See that what you're getting output 10 00:00:31,000 --> 00:00:33,900 as, 'ModuleNotFoundError:'. 11 00:00:33,900 --> 00:00:37,700 No module name called your 'getpasspass' because not only 12 00:00:37,700 --> 00:00:41,000 this, if any module is not present with your Python software, 13 00:00:41,000 --> 00:00:42,700 it will throw this error. 14 00:00:43,500 --> 00:00:47,400 Right. If this module is there, then it won't give any error. Then immediately 15 00:00:47,600 --> 00:00:52,300 we know that we can run 'getpass', 'dir' of that, so that it is 16 00:00:52,300 --> 00:00:55,500 going to list all your functions and variables which are 17 00:00:55,500 --> 00:00:56,600 there with your module. 18 00:00:56,800 --> 00:01:00,400 But if we observe here, there are two modules, two functions 19 00:01:00,400 --> 00:01:02,700 with this. They are like 'getpass' and 'getuser'. 20 00:01:02,700 --> 00:01:06,200 These are very, very important from this 'getpass' module. Right. 21 00:01:06,500 --> 00:01:09,700 See that. Then what is the usage of 'getpass()'? 22 00:01:10,300 --> 00:01:16,400 See, generally to read some string we are using 'input' syntax 23 00:01:16,400 --> 00:01:21,900 in your Python, and with 'input' syntax, whatever you are typing 24 00:01:21,900 --> 00:01:23,200 on your command line, 25 00:01:23,200 --> 00:01:26,700 I mean while providing 'input' it is displaying on your terminal. 26 00:01:27,300 --> 00:01:29,400 Let me write simple Python script first. 27 00:01:30,200 --> 00:01:31,200 My intention is, 28 00:01:31,200 --> 00:01:35,200 [no audio] 29 00:01:35,200 --> 00:01:38,700 I want to read a password, 'read_pass'. 30 00:01:39,600 --> 00:01:44,100 Right. For that I am going to write a simple Python script. Be clear. Here 31 00:01:44,100 --> 00:01:48,800 my intention is, I want to read a password. See you know that 32 00:01:48,800 --> 00:01:51,800 whenever if you are reading a password while typing 33 00:01:51,800 --> 00:01:54,900 that password on your command line, if you don't display 34 00:01:54,900 --> 00:01:56,900 then that is good. Right. 35 00:01:56,900 --> 00:02:00,800 First of all, let me do one thing. 'my_pass' or some 'db_pass' 36 00:02:00,800 --> 00:02:05,800 I am taking. 'db_pass = input("Enter your password:)'. 37 00:02:05,800 --> 00:02:08,400 [no audio] 38 00:02:08,400 --> 00:02:11,199 Right. Let me save it and run it. 39 00:02:11,199 --> 00:02:13,209 [no audio] 40 00:02:13,199 --> 00:02:17,200 So, I'm going to run this script, 'read_pass.py'. "Enter your 41 00:02:17,200 --> 00:02:20,900 password: ". I'm entering suppose some password, "db123" password. 42 00:02:21,500 --> 00:02:25,900 So actually, if it is password try to avoid displaying your 43 00:02:25,900 --> 00:02:28,700 password while typing on your command line, then that is 44 00:02:28,700 --> 00:02:29,700 a good practice. 45 00:02:30,100 --> 00:02:33,400 But with your normal 'input' syntax, you can't do that. 46 00:02:34,700 --> 00:02:38,400 Right. That's why what I am doing is, see that, what I am doing. 47 00:02:38,600 --> 00:02:40,900 I am going to import a module called. 48 00:02:40,900 --> 00:02:43,109 [no audio] 49 00:02:43,100 --> 00:02:46,700 Let me comment this line, or no problem just delete it. 50 00:02:47,800 --> 00:02:49,700 Try to import first 'getpass' module. 51 00:02:50,200 --> 00:02:55,500 Then I am going to read 'db_pass'. First observe the code. 52 00:02:55,900 --> 00:02:59,000 'getpass.getpass', right? That's it. 53 00:02:59,000 --> 00:03:00,900 [no audio] 54 00:03:00,900 --> 00:03:03,800 Now see that, I am going to run this script. 55 00:03:04,700 --> 00:03:06,300 Right. Let me run it. 56 00:03:07,000 --> 00:03:08,000 I am running. 57 00:03:08,800 --> 00:03:11,400 See, it's asking by default 'Password:' as the prompt, 58 00:03:11,400 --> 00:03:13,600 but I am typing here you are not able to see that. 59 00:03:15,000 --> 00:03:18,800 Done. So, whatever you are typing, in case if you want to see just 60 00:03:18,800 --> 00:03:23,600 for verification, just for verification after reading just 61 00:03:23,600 --> 00:03:25,600 try to print that, so that it will be 62 00:03:25,600 --> 00:03:27,600 [no audio] 63 00:03:27,600 --> 00:03:29,300 clear to understand this. 64 00:03:29,900 --> 00:03:33,300 Right. So 'print', I will do one thing. 65 00:03:33,300 --> 00:03:35,910 [no audio] 66 00:03:35,900 --> 00:03:42,900 "The entered password is :". Right. I'm going to print that, so 67 00:03:42,900 --> 00:03:46,400 that I can get clarity whether it is reading password or not, right? 68 00:03:47,600 --> 00:03:49,300 Now let me run this. 69 00:03:49,800 --> 00:03:50,800 So, I'm going to run it. 70 00:03:51,700 --> 00:03:52,700 What happened? 71 00:03:52,700 --> 00:03:54,600 Okay, I started with single quotation, 72 00:03:54,700 --> 00:03:57,100 sorry, double quotation and ended with the single quotation 73 00:03:57,100 --> 00:03:58,400 in your 'print' statement. 74 00:03:59,300 --> 00:04:01,100 So, now it's good. 75 00:04:01,100 --> 00:04:03,310 [no audio] 76 00:04:03,300 --> 00:04:05,600 Now I am running. See the output. 77 00:04:06,400 --> 00:04:08,700 First of all, it is asking 'Password: '. Guys, 78 00:04:09,400 --> 00:04:11,200 it's bad to see code as well, right? 79 00:04:11,200 --> 00:04:13,200 [no audio] 80 00:04:13,200 --> 00:04:16,700 'read_pass.py', this is your code. What you are doing? 81 00:04:16,800 --> 00:04:20,000 Very first you are importing 'getpass'. Why I am importing 82 00:04:20,000 --> 00:04:22,500 this 'getpass' module? Because I'm using that in my code, 83 00:04:22,600 --> 00:04:25,100 that's why before using always you have to import. 84 00:04:25,100 --> 00:04:26,100 Yes, I did it. 85 00:04:26,600 --> 00:04:29,400 Now, this is like your 'input' syntax only. You have 'input', right, 86 00:04:29,400 --> 00:04:31,900 same way 'getpass.getpass()', with parentheses. 87 00:04:32,400 --> 00:04:36,600 This is a function. Now, because of this, right, what you are going to 88 00:04:36,600 --> 00:04:41,100 get is, you're going to get some prompt, that is 'Password: '. 89 00:04:41,200 --> 00:04:45,900 Now, I'm entering 'db', some password. See the result. Now, 90 00:04:45,900 --> 00:04:48,200 let me rerun, and I'm going to provide, suppose 91 00:04:48,200 --> 00:04:51,000 let's say 'python' as password. That's it. 92 00:04:52,200 --> 00:04:54,900 So, while giving your password to your script you are not 93 00:04:54,900 --> 00:04:57,500 displaying that, you are not echoing on your terminal. That 94 00:04:57,500 --> 00:05:00,500 is actually good practice whenever if you are providing password. 95 00:05:01,100 --> 00:05:02,400 Right. Fine. 96 00:05:03,600 --> 00:05:10,200 Now, see by default your 'getpass' is giving prompt as 'Password: ', 97 00:05:11,000 --> 00:05:15,100 but instead of 'Password: ', if you want to provide any other 98 00:05:15,400 --> 00:05:19,400 prompt, yes, you can provide it. Right. See for that 99 00:05:19,400 --> 00:05:21,700 what you have to do is, nothing is there. Simply write, 100 00:05:22,800 --> 00:05:26,200 'prompt = "Enter your db pass:"'. 101 00:05:26,200 --> 00:05:29,000 [no audio] 102 00:05:29,000 --> 00:05:31,900 That's it. Now, let me save it and run it. 103 00:05:31,900 --> 00:05:32,900 [no audio] 104 00:05:32,900 --> 00:05:36,300 And see the output now what you are going to get. "Enter your 105 00:05:36,300 --> 00:05:41,200 db pass: ". By default 'getpass' is giving prompt as 'Password:', 106 00:05:41,400 --> 00:05:44,700 but if you want to change your prompt you can change it in this way. 107 00:05:46,100 --> 00:05:47,100 That's it. 108 00:05:48,000 --> 00:05:53,000 Right. So, this is the way to read your password in your real time. 109 00:05:53,700 --> 00:05:58,100 Right. See guys, whatever the code you are having here same code 110 00:05:58,100 --> 00:06:00,600 you can run on your Unix-like systems as well. 111 00:06:00,600 --> 00:06:01,700 Let me copy it. 112 00:06:02,200 --> 00:06:04,200 I am going to open my Linux box. 113 00:06:05,800 --> 00:06:09,500 See this is Linux, right? Sorry. 114 00:06:11,000 --> 00:06:18,600 Linux. Now what I am doing is, 'read_pass.py', copy and paste it. 115 00:06:19,800 --> 00:06:25,200 Save it. Run this. So on my Unix I have given my 'python3' as 116 00:06:25,200 --> 00:06:30,900 'python3'. Not like Windows, right? Then, yes, it's working, right? 117 00:06:30,900 --> 00:06:32,799 [no audio] 118 00:06:32,799 --> 00:06:36,200 But in your Unix-like systems, you know, you have a shebang 119 00:06:36,200 --> 00:06:40,500 line concept, you can add your Python path at very first line 120 00:06:40,500 --> 00:06:46,900 in a script in this way. That is '#!', then 121 00:06:46,900 --> 00:06:51,100 your path of Python, then provide the execution permission 122 00:06:51,100 --> 00:06:52,300 for your script, 123 00:06:52,800 --> 00:06:55,400 then you can run now your script in this way, instead of providing 124 00:06:55,400 --> 00:06:57,300 'python', space, your script name. 125 00:06:57,900 --> 00:06:58,900 That's it. 126 00:06:58,900 --> 00:07:00,500 [no audio] 127 00:07:00,500 --> 00:07:01,500 Right. Fine. 128 00:07:02,200 --> 00:07:07,000 So, guys that is about 'getpass()' from your 'getpass' module, 129 00:07:07,100 --> 00:07:08,800 but you are also having 'getuser'. 130 00:07:09,600 --> 00:07:12,400 So, this is also a function. The purpose of this function 131 00:07:12,500 --> 00:07:17,700 is, it displays the login name of the user. This function checks 132 00:07:17,700 --> 00:07:21,500 the environment variables like LOGNAME, USER, and USERNAME, 133 00:07:21,500 --> 00:07:23,900 in order, in order, right? 134 00:07:24,600 --> 00:07:27,000 So first it will go and check about LOGNAME. 135 00:07:27,000 --> 00:07:28,300 Is there any value set for 136 00:07:28,300 --> 00:07:30,700 LOGNAME variable in your operating system, then it will 137 00:07:30,700 --> 00:07:32,000 fetch that as a 'username'. 138 00:07:32,400 --> 00:07:36,100 Then it will go, in case if it is not there, LOGNAME variable 139 00:07:36,100 --> 00:07:40,100 is not there, or if it is empty then your Python will go and 140 00:07:40,100 --> 00:07:42,500 check 'USER' variable. Likewise 141 00:07:42,500 --> 00:07:46,700 it will check all those things, see, and finally it returns 142 00:07:46,700 --> 00:07:49,100 the value of the first non-empty string. 143 00:07:49,800 --> 00:07:53,000 See you have here multiple variables from your operating system. 144 00:07:53,000 --> 00:07:55,800 These all are your operating system variables. 145 00:07:55,800 --> 00:07:57,600 [no audio] 146 00:07:57,600 --> 00:08:00,700 So while using this function Python what it will do means, 147 00:08:00,800 --> 00:08:02,600 Python will go and check this value. 148 00:08:02,900 --> 00:08:06,700 If this value is set that value it will return, if it is not 149 00:08:06,700 --> 00:08:09,700 there or if it is empty then by then will go and check 'USER' 150 00:08:09,700 --> 00:08:12,700 variable, likewise. Right. See that. 151 00:08:13,300 --> 00:08:15,100 Let me open my terminal here. 152 00:08:15,800 --> 00:08:21,400 So guys by default if you observe, there is a who, suppose 153 00:08:21,400 --> 00:08:23,700 in Unix-like systems you have a command called, 154 00:08:24,700 --> 00:08:29,200 'whoami'. Sorry, 'whoami'. 'ec2-user'. 155 00:08:29,700 --> 00:08:33,799 So, I logged with this system with 'ec2-user', right. Now 156 00:08:33,799 --> 00:08:35,600 through Python also you can get it. 157 00:08:35,900 --> 00:08:38,200 Let me directly show you from your Python terminal. 158 00:08:38,799 --> 00:08:41,200 So 'import getpass'. 159 00:08:42,000 --> 00:08:48,400 Let me print or, yeah, 'getpass.getuser()'. 160 00:08:49,600 --> 00:08:51,000 That's it. See the output. 161 00:08:51,000 --> 00:08:55,710 [no audio] 162 00:08:55,700 --> 00:08:59,400 Right. So I'm not sure whether you know this command on your 163 00:08:59,400 --> 00:09:00,500 Windows or not. 164 00:09:00,500 --> 00:09:05,900 If you run 'env' command somewhere you have, let me 'grep' it. 165 00:09:05,900 --> 00:09:07,810 [no audio] 166 00:09:07,800 --> 00:09:10,600 'grep ec2-user'. 167 00:09:10,600 --> 00:09:12,500 [no audio] 168 00:09:12,500 --> 00:09:14,600 See, 'LOGNAME' is there, right, 169 00:09:14,600 --> 00:09:16,560 [no audio] 170 00:09:16,550 --> 00:09:18,200 or 'USER' variable is there, right? 171 00:09:18,900 --> 00:09:22,100 So, what your Python will do means, whenever if you are running 172 00:09:22,500 --> 00:09:26,200 your command 'getpass.getuser()' from your Python, 173 00:09:26,200 --> 00:09:28,700 your Python will come to and interact with your operating 174 00:09:28,700 --> 00:09:32,800 system variables, like 'USER', right, 'LOGNAME', and whatever the 175 00:09:32,800 --> 00:09:35,700 value we have set for this by your operating system that 176 00:09:35,700 --> 00:09:37,300 value your Python will display. 177 00:09:38,800 --> 00:09:43,900 Right. So, guys simply the advantage of your 'getuser()' is nothing 178 00:09:43,900 --> 00:09:48,100 but automatically you can find out the login user for your 179 00:09:48,100 --> 00:09:49,100 operating system. 180 00:09:49,900 --> 00:09:51,700 That's it. Okay. 181 00:09:52,700 --> 00:09:55,700 So instead of 'getuser() actually 'getpass()' is very, very important 182 00:09:55,700 --> 00:09:58,100 because just to read your password without echoing while 183 00:09:58,100 --> 00:10:00,700 typing on your command line, right? 184 00:10:01,100 --> 00:10:03,100 Okay guys, thank you for watching this video. 185 00:10:03,100 --> 00:10:11,200 [no audio]