Sqlite3命令在Python命令行中有效,但在CGI中不起作用

时间:2019-05-02 07:55:21

标签: python sqlite cgi

我将值插入python shell的sqlite3表中(效果很好),但没有插入CGI脚本中。

#!/usr/bin/python3
import cgi,cgitb
import sqlite3


cgitb.enable()

print('Content-type: text/html\r\n\r\n')
print('<html>')
print('<head><title>Registration</title></head>')
print('<body>')

form = cgi.FieldStorage()
if form.getvalue('name'):
    name=str(form.getvalue('name'))
else:
    name=None
if form.getvalue('email'):
    email=str(form.getvalue('email'))
else:
    email=None
if form.getvalue('password'):
    password=str(form.getvalue('password'))
else:
    password=None

try:
    con = sqlite3.connect('users.db')
    cur=con.cursor()
    val=(name,email,password)
    with con:
            sql='''INSERT OR REPLACE INTO USER_INFO(NAME,EMAIL,PASSWORD) VALUES(?,?,?)'''
            cur.execute(sql, val)
    con.commit()

    htmlf='''<html><title>Registration</title><body><p>Account {email} Stored in Record</p></body></html>'''
    print(htmlf.format(email=email))
    con.commit()

except sqlite3.Error:
    if con:
           con.rollback()
con.close()

print("<form method='post' action='h.py'>")
print("<p>Registration</p>")
print("<p>Enter Name: <input type='text' name='name' /></p>")  
print("<p>Enter E-mail ID: <input type='text' name='email' /></p>")
print("<p>Enter Password:<input type='password' name='password' /></p>")
print("<input type='submit' value='Submit' />")
print("</form>")
print("</body>")
print("</html>")

如果我使用python shell插入值,则可以正常工作。 但不在CGI脚本中 我将sqlite3数据库文件的模式更改为777,并将数据库放在此CGI python文件的同一目录中。 没用

在数据库目录和数据库文件中发现问题之后。 使用此命令,

chgrp www-data databasedir chgrp www-data databasedir/mydb.db chmod g+w databasedir chmod g+w databasedir/mydb.db

工作正常

我有疑问,为什么insert sqlite3命令在python shell(作为超级用户的cd到数据库目录的路径)中工作并且在cgi脚本中工作,但是更改了数据库的权限之后 任何帮助,谢谢

0 个答案:

没有答案