0

我昨天发布了一个问题: 将 python 脚本重定向到另一个 python 脚本以验证登录凭据

在我完成了那一点之后,我遇到了另一个与 cookie 有关的问题。我从 python 脚本代码中发送了标头中的 cookie:

        #Create Cookie
        C= Cookie.SimpleCookie()
        #take the value of usernameLogin into the variable username
        username= form.getvalue('usernameLogin')
        #Set-Cookie header with the usernameLogin key
        C['usernameLogin'] = username

这段代码在前面的 python 脚本validate.py中

我想将 cookie 发送到下一个脚本page1.py 这是我的page1.py代码:

import cgi
import cgitb
import sqlite3
import Cookie
import os

user_name=""
user_id=""
useridDb=""
resultid=""
resultname=""
idUser=""
if os.environ.has_key("HTTP_COOKIE"):
    C= Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE",""))
    if C.has_key("usernameLogin"):
        user_name= C['usernameLogin'].value
        print user_name

form= cgi.FieldStorage()
cgitb.enable()

#Open connection
conn= sqlite3.connect("manager.db")


page1head= """

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Excursions</title>
    <link rel='stylesheet' type='text/css' href='/page1.css/' />
    <script lang="javascript" type="text/javascript" src="/suggestions.js/"> </script>

</head><body>

    <div id="container">
        <div id="header">
            <h1>Field Note Manager</h1>
            <p class="description">Observe...Record...Save!</p>
        </div>

        <!-- Content Section -->
        <div id="wrapper">
        <!-- Main Content Section -->
            <div id="content">

                <h2>Excursions</h2>


                    <table CELLPADDING="10" ><tr><th> <strong> Location </strong> </th><th> <strong> Date </strong> </th> <th> Time </th></tr>"""

page2head="""</table>


            </div>
        </div>

        <!-- Logout Section -->
        <div id="navigation">

             <input type="hidden" name="greeting"/>
                         <form action="http://localhost:8000/cgi-bin/logout.py">
             <p><input type="submit" name="logoutBtn" value="Logout" /> </p>
            </form>
        </div>

        <!-- Extra Section for Queries -->
        <div id="extra">
            <h2>Quick Links</h2>            


            <dl> <dd><a href="http://localhost:8000/cgi-bin/query.py"/>Query the Database</a> </dd></dl>
            <dl> <dd><a href="http://localhost:8000/cgi-bin/addFieldNote.py"/>Add Field Note</a> </dd></dl>


        </div>

        <!-- Footer -->
        <div id="footer">
            <p>Copyright  42578647, 2012</p>
        </div> 
    </div>
"""

page1foot= """

</body>
</html>
"""


print "Content_type: text/html\n\n"
print page1head

#print excursion details
cur=conn.cursor()
resultid= cur.execute("SELECT userid FROM login WHERE username=?",[user_name])
cur.fetchone()
for data in resultid:
    idUser= int(data)
resultname= cur.execute("""SELECT location,excurDate,excurTime FROM excursion WHERE user=?""",[idUser])
cur.fetchall()
for record in resultname:
    print"<tr><td>",record[0],"</td><td>",record[1],"</td><td>",record[2],"</td></tr>"
print page2head
print page1foot

它打印页面,但不生成查询。是因为 cookie 没有被检索和解析,还是 SELECT 语句有问题?

4

1 回答 1

0

我找到了答案。在之前的 validate.py 中的 header 之前没有设置 cookie

于 2012-04-27T04:51:37.733 回答