🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The getpass module in Python provides a way to securely handle password prompts in a command-line interface. It avoids showing the input on the screen, making it ideal for entering passwords or other sensitive information.
Table of Contents
- Introduction
- Key Functions
getpassgetuser
- Examples
- Basic Password Prompt
- Using
getpassin a Script - Retrieving the Current User
- Real-World Use Case
- Conclusion
- References
Introduction
The getpass module is used to securely prompt the user for a password without echoing the input back to the console. It is commonly used in scripts and applications that require user authentication or other sensitive input.
Key Functions
getpass
Prompts the user for a password without echoing.
import getpass
password = getpass.getpass('Enter your password: ')
print(f'You entered: {password}')
getuser
Returns the username of the current user.
import getpass
username = getpass.getuser()
print(f'Current user: {username}')
Examples
Basic Password Prompt
import getpass
password = getpass.getpass('Enter your password: ')
print('Password received.')
Example Run:
Enter your password:
Password received.
Using getpass in a Script
import getpass
def authenticate():
user = getpass.getuser()
password = getpass.getpass('Enter your password: ')
# Simulate authentication (replace with actual authentication logic)
if password == 'secret':
print(f'Authentication successful. Welcome, {user}!')
else:
print('Authentication failed.')
authenticate()
Example Run:
Enter your password:
Authentication successful. Welcome, <current_user>!
Retrieving the Current User
import getpass
username = getpass.getuser()
print(f'Current user: {username}')
Output:
Current user: <current_user>
Real-World Use Case
Secure User Authentication
You can use the getpass module to prompt for a username and password in a secure way and then authenticate the user.
import getpass
def authenticate_user():
username = input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
# Replace with actual authentication logic
if username == 'admin' and password == 'admin123':
print('Authentication successful!')
else:
print('Authentication failed!')
if __name__ == '__main__':
authenticate_user()
Example Run:
Enter your username: admin
Enter your password:
Authentication successful!
Conclusion
The getpass module in Python is used for handling password prompts securely in command-line interfaces. By using this module, you can ensure that sensitive information such as passwords is not displayed on the screen.
References
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment