Python: Extend the script below so it maintains a password le. For every user th
ID: 3675881 • Letter: P
Question
Python: Extend the script below so it maintains a password le. For every user there is one line: two strings (name and password) separated by a colon.
from ast import literal_eval
import cgi
FORM = cgi.FieldStorage()
print("Content-Type: text/plain ")
ERROR = False
try:
NAME = FORM['login'].value
except KeyError:
print("please enter your name")
ERROR = True
try:
WORD = FORM['password'].value
except KeyError:
print("please enter a password")
ERROR = True
if not ERROR:
if literal_eval(FORM['new'].value):
print('welcome new user ' + NAME)
else:
print('welcome back ' + NAME)
Explanation / Answer
from ast import literal_eval
import cgi
FORM = cgi.FieldStorage()
f=file('passwd.txt','a')
print("Content-Type: text/plain ")
ERROR = False
try:
NAME = FORM['login'].value
f.write(NAME+";")
except KeyError:
print("please enter your name")
ERROR = True
try:
WORD = FORM['password'].value
f.write(WORD+" ")
except KeyError:
print("please enter a password")
ERROR = True
if not ERROR:
if literal_eval(FORM['new'].value):
print('welcome new user ' + NAME)
else:
print('welcome back ' + NAME)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.