Type 'exceptions.AttributeError': 'list' object has no attribute 'value'

StackOverflow https://stackoverflow.com/questions/23351462

  •  11-07-2023
  •  | 
  •  

سؤال

i am working on python cgi script and I am not able to resolve this error. Not sure what I am doing wrong. If someone can help me fix the error, will highly appreciate it.

the error is on line

    --> action=form["action"].value

here is the entire code

#!/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()
هل كانت مفيدة؟

المحلول

Because your form has two rows for action:

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

Then, form["action"] be an list.

You can print out that with print(form["action"]), i think output is ['login', 'register']

cgi.html help:

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top