gpt4 book ai didi

python - 类型 'exceptions.AttributeError' : 'list' object has no attribute 'value'

转载 作者:行者123 更新时间:2023-12-01 05:15:24 24 4
gpt4 key购买 nike

我正在处理 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()

最佳答案

因为您的表单有两行用于操作:

<INPUT TYPE=hidden NAME="action" VALUE="login">
<INPUT TYPE=hidden NAME="action" VALUE="register">

然后,form["action"] 是一个列表。

你可以用print(form["action"])打印出来,我认为输出是['login', 'register']

cgi.html帮助:

If the submitted form data contains more than one field with the samename, the object retrieved by form[key] is not a FieldStorage orMiniFieldStorage instance but a list of such instances.

Similarly, in this situation, form.getvalue(key) would return a list of strings. Ifyou expect this possibility (when your HTML form contains multiplefields with the same name), use the getlist() method, which alwaysreturns a list of values (so that you do not need to special-case thesingle item case).

关于python - 类型 'exceptions.AttributeError' : 'list' object has no attribute 'value' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23351462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com