[no audio]
Friends, here we are going to discuss about 'getpass' module.
See this 'getpass' module consists of two important functions,
they are 'getpass()' and 'getuser()'.
See you know that if you want to see what are the different
functions are there with your module what you have to do?
Simply first enter into your Python, then try to import that
module, 'getpass'. But I am giving some wrong module name
'getpasspass', something. See that what you're getting output
as, 'ModuleNotFoundError:'.
No module name called your 'getpasspass' because not only
this, if any module is not present with your Python software,
it will throw this error.
Right. If this module is there, then it won't give any error. Then immediately
we know that we can run 'getpass', 'dir' of that, so that it is
going to list all your functions and variables which are
there with your module.
But if we observe here, there are two modules, two functions
with this. They are like 'getpass' and 'getuser'.
These are very, very important from this 'getpass' module. Right.
See that. Then what is the usage of 'getpass()'?
See, generally to read some string we are using 'input' syntax
in your Python, and with 'input' syntax, whatever you are typing
on your command line,
I mean while providing 'input' it is displaying on your terminal.
Let me write simple Python script first.
My intention is,
[no audio]
I want to read a password, 'read_pass'.
Right. For that I am going to write a simple Python script. Be clear. Here
my intention is, I want to read a password. See you know that
whenever if you are reading a password while typing
that password on your command line, if you don't display
then that is good. Right.
First of all, let me do one thing. 'my_pass' or some 'db_pass'
I am taking. 'db_pass = input("Enter your password:)'.
[no audio]
Right. Let me save it and run it.
[no audio]
So, I'm going to run this script, 'read_pass.py'. "Enter your
password: ". I'm entering suppose some password, "db123" password.
So actually, if it is password try to avoid displaying your
password while typing on your command line, then that is
a good practice.
But with your normal 'input' syntax, you can't do that.
Right. That's why what I am doing is, see that, what I am doing.
I am going to import a module called.
[no audio]
Let me comment this line, or no problem just delete it.
Try to import first 'getpass' module.
Then I am going to read 'db_pass'. First observe the code.
'getpass.getpass', right? That's it.
[no audio]
Now see that, I am going to run this script.
Right. Let me run it.
I am running.
See, it's asking by default 'Password:' as the prompt,
but I am typing here you are not able to see that.
Done. So, whatever you are typing, in case if you want to see just
for verification, just for verification after reading just
try to print that, so that it will be
[no audio]
clear to understand this.
Right. So 'print', I will do one thing.
[no audio]
"The entered password is :". Right. I'm going to print that, so
that I can get clarity whether it is reading password or not, right?
Now let me run this.
So, I'm going to run it.
What happened?
Okay, I started with single quotation,
sorry, double quotation and ended with the single quotation
in your 'print' statement.
So, now it's good.
[no audio]
Now I am running. See the output.
First of all, it is asking 'Password: '. Guys,
it's bad to see code as well, right?
[no audio]
'read_pass.py', this is your code. What you are doing?
Very first you are importing 'getpass'. Why I am importing
this 'getpass' module? Because I'm using that in my code,
that's why before using always you have to import.
Yes, I did it.
Now, this is like your 'input' syntax only. You have 'input', right,
same way 'getpass.getpass()', with parentheses.
This is a function. Now, because of this, right, what you are going to
get is, you're going to get some prompt, that is 'Password: '.
Now, I'm entering 'db', some password. See the result. Now,
let me rerun, and I'm going to provide, suppose
let's say 'python' as password. That's it.
So, while giving your password to your script you are not
displaying that, you are not echoing on your terminal. That
is actually good practice whenever if you are providing password.
Right. Fine.
Now, see by default your 'getpass' is giving prompt as 'Password: ',
but instead of 'Password: ', if you want to provide any other
prompt, yes, you can provide it. Right. See for that
what you have to do is, nothing is there. Simply write,
'prompt = "Enter your db pass:"'.
[no audio]
That's it. Now, let me save it and run it.
[no audio]
And see the output now what you are going to get. "Enter your
db pass: ". By default 'getpass' is giving prompt as 'Password:',
but if you want to change your prompt you can change it in this way.
That's it.
Right. So, this is the way to read your password in your real time.
Right. See guys, whatever the code you are having here same code
you can run on your Unix-like systems as well.
Let me copy it.
I am going to open my Linux box.
See this is Linux, right? Sorry.
Linux. Now what I am doing is, 'read_pass.py', copy and paste it.
Save it. Run this. So on my Unix I have given my 'python3' as
'python3'. Not like Windows, right? Then, yes, it's working, right?
[no audio]
But in your Unix-like systems, you know, you have a shebang
line concept, you can add your Python path at very first line
in a script in this way. That is '#!', then
your path of Python, then provide the execution permission
for your script,
then you can run now your script in this way, instead of providing
'python', space, your script name.
That's it.
[no audio]
Right. Fine.
So, guys that is about 'getpass()' from your 'getpass' module,
but you are also having 'getuser'.
So, this is also a function. The purpose of this function
is, it displays the login name of the user. This function checks
the environment variables like LOGNAME, USER, and USERNAME,
in order, in order, right?
So first it will go and check about LOGNAME.
Is there any value set for
LOGNAME variable in your operating system, then it will
fetch that as a 'username'.
Then it will go, in case if it is not there, LOGNAME variable
is not there, or if it is empty then your Python will go and
check 'USER' variable. Likewise
it will check all those things, see, and finally it returns
the value of the first non-empty string.
See you have here multiple variables from your operating system.
These all are your operating system variables.
[no audio]
So while using this function Python what it will do means,
Python will go and check this value.
If this value is set that value it will return, if it is not
there or if it is empty then by then will go and check 'USER'
variable, likewise. Right. See that.
Let me open my terminal here.
So guys by default if you observe, there is a who, suppose
in Unix-like systems you have a command called,
'whoami'. Sorry, 'whoami'. 'ec2-user'.
So, I logged with this system with 'ec2-user', right. Now
through Python also you can get it.
Let me directly show you from your Python terminal.
So 'import getpass'.
Let me print or, yeah, 'getpass.getuser()'.
That's it. See the output.
[no audio]
Right. So I'm not sure whether you know this command on your
Windows or not.
If you run 'env' command somewhere you have, let me 'grep' it.
[no audio]
'grep ec2-user'.
[no audio]
See, 'LOGNAME' is there, right,
[no audio]
or 'USER' variable is there, right?
So, what your Python will do means, whenever if you are running
your command 'getpass.getuser()' from your Python,
your Python will come to and interact with your operating
system variables, like 'USER', right, 'LOGNAME', and whatever the
value we have set for this by your operating system that
value your Python will display.
Right. So, guys simply the advantage of your 'getuser()' is nothing
but automatically you can find out the login user for your
operating system.
That's it. Okay.
So instead of 'getuser() actually 'getpass()' is very, very important
because just to read your password without echoing while
typing on your command line, right?
Okay guys, thank you for watching this video.
[no audio]