Python implements the Console Password Input Method, python Console

Source: Internet
Author: User

Python implements the Console Password Input Method, python Console

This example describes how to enter a password in the Python console. Share it with you for your reference. The details are as follows:

1. raw_input ():

pwd = raw_input('password: ')print pwd# password: aaa# aaa

Note: The simplest method, but not secure

2. getpass. getpass ():

import getpasspwd = getpass.getpass('password: ')print pwd# password:# aaaa

Note: It's safe, but you can't see the number of digits in the input. You may feel a little unaccustomed ..

3. msvcrt. getch ():

The Code is as follows:

Import msvcrt, sysdef pwd_input (): chars = [] while True: newChar = msvcrt. getch () if newChar in '\ r \ N': # if it is a line feed, enter the end print ''break elif newChar =' \ B ': # if it is a backspace, delete the last if chars: del chars [-1] sys. stdout. write ('\ B') # delete an asterisk, but do not know why it cannot be executed... else: chars. append (newChar) sys. stdout. write ('*') # print as an asterisk ''. join (chars) pwd = pwd_input () print pwd # ****** # aaaaaa

Note: The second method does not display the number of input digits. However, if you press the backspace key,

However, the console does not display the corresponding backspace. For example, if the current input is "abcd", it is displayed as "*****", and now a backspace key is entered.

The input value is abc, but it is still :****. I don't know why sys. stdout. write ('\ B') is not executed. It is estimated that it is related to msvcrt. getch. If you are interested, you can study it further.

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.