python - 如何屏蔽用户密码输入并将其保存为输出以替换另一个文件中的默认密码
问题描述
我是python新手,真的不知道如何正确提问,所以请原谅我。
真正需要指导的是,我想让用户使用输入输入他的密码,输出被屏蔽为星号,用户输入将用于更改另一个文件信息(我在 Windows 10 上使用 python 3)。
我知道我可以使用 getpass.getpass() 并将密码显示为星号是有风险的,因为它让其他人阅读并计算密码的长度,但是因为我想让这个小应用程序看起来“专业”并让用户感到安全我看到需要屏蔽密码。
最后应该保存输出以替换另一个文件基本信息,例如电子邮件、用户 ID 和密码。
我已经成功替换了除密码之外的所有密码,因为它只将输入的密码(mynewpassword12345)更改为 "" 。
以下是我从 SO 获得的一些链接,据说与我的问题有关:
用星号接受的答案在 python 中屏蔽用户输入 对于如何或不提供示例没有方向。
获取路径作为用户输入 Python 3 这个接受的答案与我的问题无关。
只输入 3 个字符和 3 个星号, 这个接受的答案也与我的问题无关。
将 EditText 密码掩码字符更改为星号 (*) 这个接受的答案也与我的问题无关。
输入字段上的部分密码屏蔽 这个接受的答案再次与我的问题无关。
在 python 中用星号屏蔽用户输入 这个接受的答案与我的问题有关,但在 Windows 以外的平台上。
字符串到星号,再次屏蔽密码 这些答案与我的问题无关。
字(密码)以星号显示 - 控制台应用程序 再次接受此答案与我的问题无关。
下面是我从 SO 得到的示例代码示例,将用户密码屏蔽为星号(但很抱歉我忘记保存链接)。
它在 python exe 上工作(但不在空闲状态下)并将用户密码输入屏蔽为星号,但它不允许我将打印的密码保存为用户输出。
到目前为止,这就是我想要做的,从特定路径中的 certin 文件夹中提取一个 zip 文件,提示用户输入并添加以更改默认信息,即电子邮件、ID 等。
from zipfile import ZipFile
from selenium.webdriver.support.ui import WebDriverWait
import fileinput
import os
import py_compile
import shutil, glob
import textwrap
import datetime
import msvcrt
import time
import sys
script_dir = os.path.dirname(__file__)
file_path2 = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , 'myapp.zip')
##print(file_path2)
NameFile2 = file_path2
with ZipFile(NameFile2, 'r') as zf:
zf.extractall(os.path.join(script_dir, 'alert' , 'temp' , 'temp folder' , 'temp' ) , pwd=b'mypassword' )
file_path = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , 'myapp.py')
##print(file_path)
NameFile = file_path
email = "emailaddress"
password = "userpassword"
userID = "userID"
id1 = "id1"
id2 = "id2"
id3 = "id3"
id4 = "id4"
id5 = "id5"
id6 = "id6"
id7 = "id7"
id8 = "id8"
id9 = "id9"
idlast = "idlast"
level = 3
##
x = datetime.datetime.now()
text1 = '''hi, welcome to my app.
'''
#print('\t' * level, text1 )
#print(textwrap.fill(text1, 60))
print(" ")
print((x.strftime("%c")).center(80))
print(" ")
for line in textwrap.wrap(text1, width=50):
print(line.center(80))
print(" ")
newemail = input (" email : " )
#newpassword = input (" password : ")
list1 = []
def getPASS():
print("Input password")
list1 = []
while True:
char = msvcrt.getch()
char =str(char)
char = char[2:-1]
if char == "\\n'" or char == "\\r":
break
elif char == "\\x08":
del list1[-1]
os.system("cls")
print("Input password:")
sys.stdout.write("*" * len(list1))
sys.stdout.flush()
continue
else:
list1.append(char)
sys.stdout.write("*")
sys.stdout.flush()
print("\n")
psw = "".join(list1)
print(psw)
invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
for x in psw:
if x in invalid:
print("Character %r is not allowed in password" % x)
getPASS()
else:
pass
getPASS()
newpassword = psw
newuserID = input (" userID : ")
newid1 = input (" ID seefirst1 : ")
newid2 = input (" ID seefirst2 : ")
newid3 = input (" ID seefirst3 : ")
newid4 = input (" ID seefirst4 : ")
newid5 = input (" ID seefirst5 : ")
newid6 = input (" ID seefirst6 : ")
newid7 = input (" ID seefirst7 : ")
newid8 = input (" ID seefirst8 : ")
newid9 = input (" ID seefirst9 : ")
newidlast = input (" ID seefirst10 : ")
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(email, newemail), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(password, newpassword), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(userID, newuserID), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id1, newid1), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id2, newid2), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id3, newid3), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id4, newid4), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id5, newid5), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id6, newid6), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id7, newid7), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id8, newid8), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id9, newid9), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(idlast, newidlast), end='')
#wait = WebDriverWait(driver, 10)
file_path3 = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , 'myapp.py')
#print(file_path3)
script = file_path3
py_compile.compile(script)
from pathlib import Path
# ...
#pathtofile = Path(__file__).parent.parent.parent.parent.parent # Path to Folder myapp
pathtoduplicate = os.path.join(script_dir ,'myapp.cpython-37.pyc')
#print(pathtoduplicate)
if os.path.exists(pathtoduplicate):
os.remove(pathtoduplicate)
else:
print("The file does not exist")
##print(pathtofile)
source_dir = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , '__pycache__') #Path to Pycache
dst = os.path.join(script_dir) # Path to myapp
files = glob.iglob(os.path.join(source_dir, "*.pyc"))
for file in files:
if os.path.isfile(file):
shutil.move(file, dst)
raise SystemExit()
我得到了我希望输入显示为星号的结果,但它没有改变输出,只显示“”而不是实际密码。
想要的结果:屏蔽的输入密码已替换默认密码。
email = "mynewemail@gmail.com"
password = "mynewpassword12345"
user = "mynewnewid1"
seefirst1 = "newid1"
seefirst2 = "newid2"
seefirst3 = "newid3"
seefirst4 = "newid4"
seefirst5 = "newid5"
seefirst6 = "newid6"
seefirst7 = "newid7"
seefirst8 = "newid8"
seefirst9 = "newid9"
seefirst10 = "newid10"
不想要的结果:屏蔽的输入密码没有替换默认密码。
email = "mynewemail@gmail.com"
password = ""
user = "mynewnewid1"
seefirst1 = "newid1"
seefirst2 = "newid2"
seefirst3 = "newid3"
seefirst4 = "newid4"
seefirst5 = "newid5"
seefirst6 = "newid6"
seefirst7 = "newid7"
seefirst8 = "newid8"
seefirst9 = "newid9"
seefirst10 = "newid10"
请帮我解决这个问题。我是菜鸟,对python真的很陌生。
解决方案
撇开您方法的其他问题不谈,我将专注于您的“不需要的结果:屏蔽的输入密码没有替换默认密码”。
你有一个 global ,并且你在函数中
list1
定义了 a ,你想在从函数返回后使用它。这不能按原样工作,因为in 函数是它的本地函数,并且与 global 不同。我建议从函数中返回一个适当的值,例如:list1
getPASS()
list1
list1
def getPASS(): print("Input password") list1 = [] while True: … return "".join(list1) … psw = getPASS()
密码输入和验证的逻辑在这里混淆了:
psw = "".join(list1) print(psw) invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|' for x in psw: if x in invalid: print("Character %r is not allowed in password" % x) getPASS() else: pass getPASS()
您甚至在调用
getPASS()
输入密码之前尝试分配和验证密码,因此psw
为空。重新排列,例如(假设上面 1. 更改为getPASS()
):while True: psw = getPASS() print(psw) # only for debugging invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|' for x in psw: if x in invalid: print("Character %r is not allowed in password" % x) break # from for x in psw else: break # from while True
推荐阅读
- android - 定期同步每次运行
- datetime - Java 如何在日期时间 API 中使线程安全
- python - Python中酸洗的安全场景
- python - 通过不同列的重复值之间的条件
- java - 如何:在重新绘制之前清除 JPanel 并运行另一个类并移动对象
- android - 有什么方法可以“帮助”移动设备操作系统从 SMS 中检测代码?
- salesforce - 在 Salesforce 中将 Azure AD B2C 配置为身份验证提供程序
- javascript - 无论如何,mongodb每个月都会自动创建一个新数据库吗?
- c++ - 使用集合测试关系的对称性
- javascript - 嵌套反应路由器兄弟