简体   繁体   English

通过CGI从sqlite3检索数据时出错

[英]Error retrieving data from sqlite3 through CGI

I am trying to build a very simple login page, which asks the user for his register_no , username and password . 我正在尝试构建一个非常简单的登录页面,该页面要求用户提供他的register_nousernamepassword And when he presses the submit button. 当他按下提交按钮时。 I am trying to check whether it is an existing user or a new user and display a message accordingly. 我正在尝试检查它是现有用户还是新用户,并相应显示一条消息。

My folder hierarchy is like this 我的文件夹层次结构是这样的

prodicus@Acer:~/Downloads/souvik_refactoring$ tree
.
├── cgi-bin
│   ├── creating_user_base_table.py
│   ├── user_base.db
│   └── usr_check.py
├── index.html
└── keyCheck.py

1 directory, 5 files

What I have tried: 我试过的

For the index.html 对于index.html

<!DOCTYPE html>
<html>
<head>
    <title>Login page</title>
</head>
<body>
    <div style = "text-align : center ; ">
        <h1>Login page</h1>
        <form action="/cgi-bin/usr_check.py" method="get"> 
            Registration number : <input type="number" name="register_no" min="1" max="2000000000">
            <br><br>
            Username : <input type="text" name="username">
            <br><br>
            Password : <input type="password" name = "password">
            <br><br>
            <input type="submit" value = "Login">
        </form>
    </div>
</body>
</html>

For creating_user_base_table.py 对于creating_user_base_table.py

#!/usr/bin/env python3.4

import sqlite3
import os

db_name = "user_base.db"

if db_name in os.listdir():
    print("removing the user_base.db and creating a fresh copy of it")
    os.system("rm user_base.db")

print("Creating the database")
conn = sqlite3.connect(db_name)
cur = conn.cursor()

user_table = "CREATE TABLE users(reg_no INTEGER PRIMARY KEY, user_name TEXT, pass TEXT)"

new_users = (
    (1081310251, 'admin', 'admin'),
    (1081310234, 'foo', 'admin123')
)

cur.execute(user_table)
print("table created")
cur.executemany('INSERT INTO users VALUES(?, ?, ?)', new_users)
conn.commit()
print("default users created \n\ndisplaying them")

cur.execute('SELECT * FROM users')
print(cur.fetchall())

and finally usr_check.py 最后是usr_check.py

#/usr/bin/env python3.4

import cgi, cgitb
import os
import sqlite3

cgitb.enable()

form = cgi.FieldStorage()
register_no = form.getvalue('register_no')
username = form.getvalue('username')
passwd = form.getvalue('password')

print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<head>")
print("<h1>Shit gets real here</h1>")
print("</head>")
print("<body>")
print('<div style = "text-align:center ; "')
# print("</div>")
print("")

conn = sqlite3.connect('user_base.db')
cur = conn.cursor()

## now to check whether the entered data is for
## -> new user 
## -> an old user

cur.execute('SELECT user_name FROM users WHERE register_no = ?', (register_no,))
rows = cur.fetchall()
print("<br><br>")
if len(rows) == 0:  
    print("<p>User : <b>", username , "</b> does not exist.</p>")
    cur.execute('INSERT INTO users VALUES(?, ?, ?)', (register_no, username, passwd))
    print("<p>User was created successfully</p>")
    print("Done")

else:
    print("<p>Welcome<b>", username ,"</b>. Good to have you back")
    print("<br><p>Your account details</p>")
    print("<ul>")
    print("<li>Register number : ", register_no, " </li>")
    print("<li>Username " , username, "</li>")
    print("</ul>")

Error log : 错误日志

prodicus@Acer:~/Downloads/souvik_refactoring$ python -m CGIHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
127.0.0.1 - - [02/Nov/2015 12:43:23] "GET /index.html HTTP/1.1" 200 -
127.0.0.1 - - [02/Nov/2015 12:44:03] "GET /cgi-bin/usr_check.py?register_no=1081310234&username=foo&password=admin123 HTTP/1.1" 200 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/CGIHTTPServer.py", line 252, in run_cgi
    os.execve(scriptfile, args, env)
OSError: [Errno 8] Exec format error
127.0.0.1 - - [02/Nov/2015 12:44:03] CGI script exit status 0x7f00

Following this sqlite3 insert using python and python cgi I have the files permissions 使用sql和python cgi进行此sqlite3插入之后我具有文件许可权

prodicus@Acer:~/Downloads/souvik_refactoring$ ll
total 36
drwxrwxr-x  3 prodicus prodicus  4096 Nov  2 08:30 ./
drwxr-xr-x 15 prodicus prodicus 20480 Nov  2 11:29 ../
drwxrwxrwx  2 prodicus prodicus  4096 Nov  2 12:23 cgi-bin/
-rw-rw-r--  1 prodicus prodicus   629 Nov  2 08:38 index.html
-rwxrwxr-x  1 prodicus prodicus   463 Nov  2 08:29 keyCheck.py*

and

prodicus@Acer:~/Downloads/souvik_refactoring/cgi-bin$ ll
total 20
drwxrwxrwx 2 prodicus prodicus 4096 Nov  2 12:23 ./
drwxrwxr-x 3 prodicus prodicus 4096 Nov  2 08:30 ../
-rwxrwxrwx 1 prodicus prodicus  710 Nov  1 23:12 creating_user_base_table.py*
-rwxrwxrwx 1 prodicus prodicus 2048 Nov  2 12:23 user_base.db*
-rwxrwxrwx 1 prodicus prodicus 1576 Nov  2 08:26 usr_check.py*

Surprisingly, cgitb is not showing an error. 令人惊讶的是, cgitb没有显示错误。 Where am I going wrong? 我要去哪里错了? Have been breaking my head on this since morning! 自从早上以来,我一直对此感到震惊!

Finally figured out what was wrong with this. 终于弄清楚了这是怎么回事。 Was making a really silly mistake! 犯了一个非常愚蠢的错误! Thanks to @cas for making me come to it. 感谢@cas让我参与其中。

Had done a #/usr/bin/env python3.4 instead of #!/usr/bin/env python3.4 已经做了一个#/usr/bin/env python3.4而不是#!/usr/bin/env python3.4

Second being, I had to give the absolute path when the script usr_check.py was trying to connect to the database user_base.db 其次,当脚本usr_check.py试图连接到数据库user_base.db时,我必须给出绝对路径

That solved the problem for me. 那为我解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM