Python: Using getpass to enter password

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to Python tutorial on checking username and password so we are trying to make a small prayer class in which we are going to authenticate the user name and password and what is authentication authentication at this point I think we're gonna check user name and password it means permitting only or authorized user to get an access it can be an access to the resources to a computer system to data and information so the basic one of the basic way to authenticate access to authorized user is to give them a username and password and only the user who are having that a valid username and password will be able to enter the system we are going to focus on two skills today one is nested control story structure when while we're building up this program the other is get past module now get pass is a built in module in Python this module is very important because it helps in entering a password that will not Iko on the screen what is it what does it mean when we say don't it won't echo on the screen it means it won't be visible on the screen don't get for it I take it slow will move slow and you'll be able to understand so for example when you go to a computer lab or of your school or university you have been provided with the username and password and you enter your username and you get enter your password enter the password and they get an access to the system usually this username and password is been defined by the administrator or by the system-generated but it's not the one that you are gonna set so if we are just imagining a case like that where username has been already defined as guest and where the password hasn't been already defined as one two three and the one who will get an access to username who already knew the correct username and password will be able to enter the system so what we are going to do first thing first we're going to import get past modules so we will write on from get pass in Ford get bus know this is very special thing that is happening in this that we are using in this program and you will understand what's the purpose of this in a while so of course we have to ask our user to enter up username so we'll call it you name and we'll ask our user to enter by choose a name okay so the end I'm putting a colon and a space for clarity once it will enter a username some time user enter the right username but they put a space in the end and what happens in that case this because of that space I will get an invalid address invalid will get the condition false who it will consider that username is false what I'm gonna do is even you should be very responsible about this thing you should be variable we're about such situations that we should remove all the extra spaces in very start so it will not cause any confusion it will not give us any invalid address if you're not gonna do it right now we know the function is strip the strip function will remove all the extra spaces from start and end but if you don't want to use it now you will understand it later on and will enter it at that time okay so but the better way is test you in the even in the start whenever you're taking a you name it's better that you use the method it's a method of string so you have to as you know that for all the methods you have to define you have to call it with an with an object so here we are going to different call it the two name dot string so it will remove all the extra spaces from right and left and then it will store it that whatever the correct is whatever leftover that for better is gonna store it in you name again here comes our real program so we're gonna check we have to check the username if the user name is correct only then we'll proceed towards the password we are not gonna check the password and we would not ask for the password if the username is not correct I'm gonna write on if your name is not equal to original username so we have to use the correct spelling here if it's not equal we don't need to do anything we just need to print enter incorrect username by and we'll we'll not gonna do anything else if it you name is incorrect else if the username is correct only then will use the else statement so it's gonna go in else statement and we're gonna call we're gonna ask for the password so I'm going to say P world instead of password because password variable we have already used to predefined / password so I'm going to write on P world is equal to input I'm using input for a reason so that then I'll tell you why then I'll replace it and I'll show you what is the correct one enter your password colon space now if I run this program it will ask for the password and when the user will enter password 1 2 3 that will be visible on screen and that one thing that we don't want okay if the password is correct we'll check the password and then we'll do let's run the program till here and then we'll check for the password so that you will understand what I'm talking about so it's asking enter the username so I'm entering the username it's guest so I'm saying guest press ENTER it's asking because the username is correct so that's why it's going to else and discussing for the password so when I write on 1 2 3 it's visible on the screen you know that usually when we enter the password it's not visible and it shouldn't be visible for security reason so instead of input for that reason who instead of input we will use get past what you now you understand the purpose of get past so with guests get past the words that you will enter will not equal on the screen and instead of that there will be nothing it won't even show static okay it's for static we can use another module that is look at character gets eh and will walk with it later on but not at this point okay so P world is equal to get path so when we enter the password we're going to check the password so inside in the same in the alt statement where I'm going to check if B world is equal to equal to password which was already defined if both of them are equal then we can write down [Music] welcome over world Python and if the password is strong we are going to throw it on alt and we're going to I don't defend and other than that I don't incorrect password so it makes sense okay then we are writing first of all you should understand this point that if your name is strong use is incorrect it will just say incorrect username and it won't run the else part it means we will not even ask for the password okay and if username is correct only then we will ask for the password and will are and then we'll have the check password so if this if condition for checking the password is just nested inside this if condition ok this if condition is if if for password is nested inside if of username so it means this is how this is what is called nested control structure or National nested conditional structure in this case because it is inside another if condition it will run only if it's if the other can depending upon the condition of outer if ok let's run there is another glitch in this program another thing that we need to fix and I have discussed with you in star and we'll we'll fix it now but let's run it and see how does it work we'll do both things first will give wrong username and we'll see what will happen then we'll give correct username and we'll see what will happen so when we run and I give out wrong username so I'm giving a wrong username when I press ENTER it is not running this it's not asking for the password it's not even running this part it's just saying incorrect username which is the line 10 and finished okay now if we're going to giving the correct password username then it it will ask for the password I'm giving a correct username I'm pressing enter now because this condition became false it will go to else and it will take us for the password and for password I'm not choosing input I'm using get passed when I press 1 2 3 and I press enter it is not showing me 1 2 3 character and then it is checking if my password is equal to equal to the original is comparing it to the original password that's 1 2 3 so yes if the condition is comes true if the condition at 9:13 comes true then it will print welcome to have a world of pythons Python instead it it will say incorrect password let's run it again and this time I'm going to add on the correct username but with the space and that's what I was talking about that we should use the strip function to remove any spaces ok so for example and if I don't have this I'm making this line as a comment line 8 in which I'm removing the extra spaces as hashtag I'm putting a hashtag here so it means number sign here it will become a comment now okay so when I run the program it's asking for the username and I'm intentionally putting one two spaces and then I'm writing gasps now the user name is correct but I am putting more spaces as you can see and I'm pressing enter so it is saying incorrect username to avoid such simple errors which could be which which are human errors but which can be ignored we can use strip function so that it can remove all the extra spaces from right hand side and from left hand side from start and end of the string function and third by user so that's why were you thinking best of luck try this program and enjoy one more thing why we are not using get character program right now because I want to keep it slow and make it more keep it more understandable for you so let's keep it slow okay let's keep it more understandable to you and that's why we're just focusing on get passed and nested control structure good luck enjoy don't forget to save your work with the right name and what's right name get pass get passed dot py always give your username always give your program a name that is short and very defined descriptive you don't want a name which is long and not making any sense from the name you should be
Info
Channel: STEAM IT
Views: 2,020
Rating: undefined out of 5
Keywords:
Id: AxicSoTyMho
Channel Id: undefined
Length: 12min 28sec (748 seconds)
Published: Tue Apr 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.