我正在使用python cgi脚本,但我无法解决此错误。不确定我做错了什么。如果有人可以帮我修复错误,我将非常感激。
错误在线
--> action=form["action"].value
这是整个代码
#!/usr/bin/python
# Import the CGI, string, sys modules
import cgi, string, sys, os, re, random
import cgitb; cgitb.enable() # for troubleshooting
import sqlite3
import session
#Get Databasedir
MYLOGIN="msehgal"
DATABASE="/homes/"+MYLOGIN+"/PeteTwitt/Twitter.db"
IMAGEPATH="/homes/"+MYLOGIN+"/PeteTwitt/images"
##############################################################
# Define function to generate login HTML form.
def login_form():
html="""
<HTML>
<HEAD>
<TITLE>Info Form</TITLE>
</HEAD>
<BODY BGCOLOR = white>
<center><H2>PictureShare User Administration</H2></center>
<H3>Type User and Password:</H3>
<TABLE BORDER = 0>
<FORM METHOD=post ACTION="login.cgi">
<TR><TH>Username:</TH><TD><INPUT TYPE=text NAME="username"></TD><TR>
<TR><TH>Password:</TH><TD><INPUT TYPE=password NAME="password"></TD></TR>
</TABLE>
<INPUT TYPE=hidden NAME="action" VALUE="login">
<INPUT TYPE=submit VALUE="Enter">
<INPUT TYPE=hidden NAME="action" VALUE="register">
<INPUT TYPE=submit VALUE="Register">
</FORM>
</BODY>
</HTML>
"""
print_html_content_type()
print(html)
###################################################################
##############################################################
# Define function to generate login HTML form.
def register_form():
html="""
<HTML>
<HEAD>
<TITLE>Register From</TITLE>
</HEAD>
<BODY BGCOLOR = white>
<center><H2>Register New User</H2></center>
<TABLE BORDER = 0>
<FORM METHOD=post ACTION="register.cgi">
<input type="hidden" name="user" value="{user}">
<input type="hidden" name="session" value="{session}">
<TR><TH>Username:</TH><TD><INPUT TYPE=text NAME="username"></TD><TR>
<TR><TH>Password:</TH><TD><INPUT TYPE=password NAME="password"></TD></TR>
</TABLE>
<INPUT TYPE=hidden NAME="action" VALUE=="login">
<INPUT TYPE=submit VALUE="Register">
</FORM>
</BODY>
</HTML>
"""
print_html_content_type()
print(html)
###################################################################
# Define function to test the password.
def check_password(user, passwd):
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
t = (user,)
c.execute('SELECT * FROM users WHERE email=?', t)
row = stored_password=c.fetchone()
conn.close();
if row != None:
stored_password=row[1]
if (stored_password==passwd):
return "passed"
return "failed"
##########################################################
# Define function to test the password.
def registerUser(uname, passwrd):
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
t = (uname,passwrd)
if (c.execute('INSERT INTO users VALUES',t)):
return "passed"
else:
return "failed"
conn.commit()
conn.close();
# Diplay the options of admin
def display_admin_options(user, session):
html="""
<H1> Picture Share Admin Options</H1>
<ul>
<li> <a href="login.cgi?action=new-album&user={user}&session={session}">Create new album</a>
<li> <a href="login.cgi?action=upload&user={user}&session={session}">Upload Picture</a>
<li> <a href="login.cgi?action=show_image&user={user}&session={session}">Show Image</a>
<li> Delete album
<li> Make album public
<li> Change pasword
</ul>
"""
#Also set a session number in a hidden field so the
#cgi can check that the user has been authenticated
print_html_content_type()
print(html.format(user=user,session=session))
#################################################################
def create_new_session(user):
return session.create_session(user)
##############################################################
def new_album(form):
#Check session
if session.check_session(form) != "passed":
return
html="""
<H1> New Album</H1>
"""
print_html_content_type()
print(html);
##############################################################
def show_image(form):
#Check session
if session.check_session(form) != "passed":
login_form()
return
# Your code should get the user album and picture and verify that the image belongs to this
# user and this album before loading it
#username=form["username"].value
# Read image
with open(IMAGEPATH+'/user1/test.jpg', 'rb') as content_file:
content = content_file.read()
# Send header and image content
hdr = "Content-Type: image/jpeg\nContent-Length: %d\n\n" % len(content)
print hdr+content
###############################################################################
def upload(form):
if session.check_session(form) != "passed":
login_form()
return
html="""
<HTML>
<FORM ACTION="login.cgi" METHOD="POST" enctype="multipart/form-data">
<input type="hidden" name="user" value="{user}">
<input type="hidden" name="session" value="{session}">
<input type="hidden" name="action" value="upload-pic-data">
<BR><I>Browse Picture:</I> <INPUT TYPE="FILE" NAME="file">
<br>
<input type="submit" value="Press"> to upload the picture!
</form>
</HTML>
"""
user=form["user"].value
s=form["session"].value
print_html_content_type()
print(html.format(user=user,session=s))
#######################################################
def upload_pic_data(form):
#Check session is correct
if (session.check_session(form) != "passed"):
login_form()
return
#Get file info
fileInfo = form['file']
#Get user
user=form["user"].value
s=form["session"].value
# Check if the file was uploaded
if fileInfo.filename:
# Remove directory path to extract name only
fileName = os.path.basename(fileInfo.filename)
open(IMAGEPATH+'/user1/test.jpg', 'wb').write(fileInfo.file.read())
image_url="login.cgi?action=show_image&user={user}&session={session}".format(user=user,session=s)
print_html_content_type()
print ('<H2>The picture ' + fileName + ' was uploaded successfully</H2>')
print('<image src="'+image_url+'">')
else:
message = 'No file was uploaded'
def print_html_content_type():
# Required header that tells the browser how to render the HTML.
print("Content-Type: text/html\n\n")
##############################################################
# Define main function.
def main():
form = cgi.FieldStorage()
if "action" in form:
action=form["action"].value
#print("action=",action)
if action == "login":
if "username" in form and "password" in form:
#Test password
username=form["username"].value
password=form["password"].value
if check_password(username, password)=="passed":
session=create_new_session(username)
display_admin_options(username, session)
else:
login_form()
print("<H3><font color=\"red\">Incorrect user/password</font></H3>")
elif (action == "new-album"):
new_album(form)
elif (action == "register"):
if "username" in form and "password" in form:
username = form["username"].value
password = form["password"].value
registerUser(username,password)
else:
print("<H3>FAILED</H3>")
elif (action == "upload"):
upload(form)
elif (action == "show_image"):
show_image(form)
elif action == "upload-pic-data":
upload_pic_data(form)
else:
login_form()
else:
login_form()
###############################################################
# Call main function.
main()
答案 0 :(得分:2)
因为您的表单有两行action
:
<INPUT TYPE=hidden NAME="action" VALUE="login">
<INPUT TYPE=hidden NAME="action" VALUE="register">
然后,form["action"]
成为一个列表。
您可以使用print(form["action"])
打印出来,我认为输出为['login', 'register']
cgi.html帮助:
如果提交的表单数据包含多个具有相同字段的字段 name,由form [key]检索的对象不是FieldStorage或 MiniFieldStorage实例,但是此类实例的列表。
同样,在这种情况下,form.getvalue(key)将返回一个字符串列表。如果 您期望这种可能性(当您的HTML表单包含多个时) 具有相同名称的字段),使用getlist()方法,它总是如此 返回一个值列表(这样你就不需要特殊情况了 单项案例)。