Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python SimpleCookie检索值并在SELECT查询中使用_Python_Cookies_Cgi - Fatal编程技术网

从Python SimpleCookie检索值并在SELECT查询中使用

从Python SimpleCookie检索值并在SELECT查询中使用,python,cookies,cgi,Python,Cookies,Cgi,我昨天发布了一个问题: 在我用完那一块之后,我又遇到了另一个与饼干有关的问题。我从python脚本代码中发送了标题中的cookie: #Create Cookie C= Cookie.SimpleCookie() #take the value of usernameLogin into the variable username username= form.getvalue('usernameLogin') #

我昨天发布了一个问题:

在我用完那一块之后,我又遇到了另一个与饼干有关的问题。我从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中

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


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

我找到了答案。Cookie未在上一个validate.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