I am trying to build a very simple login page, which asks the user for his register_no, username and password. 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

<!DOCTYPE html>
    <title>Login page</title>
    <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">
            Username : <input type="text" name="username">
            Password : <input type="password" name = "password">
            <input type="submit" value = "Login">

For 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')

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

cur.execute('SELECT * FROM users')

and finally usr_check.py

#/usr/bin/env python3.4

import cgi, cgitb
import os
import sqlite3


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

print("<h1>Shit gets real here</h1>")
print('<div style = "text-align:center ; "')
# print("</div>")

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()
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("<p>Welcome<b>", username ,"</b>. Good to have you back")
    print("<br><p>Your account details</p>")
    print("<li>Register number : ", register_no, " </li>")
    print("<li>Username " , username, "</li>")

Error log :

prodicus@Acer:~/Downloads/souvik_refactoring$ python -m CGIHTTPServer
Serving HTTP on port 8000 ... - - [02/Nov/2015 12:43:23] "GET /index.html HTTP/1.1" 200 - - - [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 - - [02/Nov/2015 12:44:03] CGI script exit status 0x7f00

Following this https://stackoverflow.com/questions/10793042/sqlite3-insert-using-python-and-python-cgi I have the files permissions

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*


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. Where am I going wrong?

  • is python3.4 in your web-server's PATH? use the full path to your python3.4 interpreter on the #! line instead. – cas Nov 3 '15 at 7:03
  • 1
    is your web server configured to use fastcgi or something to handle *.py files differently? because that error message indicates it's running your script with python2.7 – cas Nov 3 '15 at 7:07
  • @cas : I did a sys.path in the python3.4 interpreter and it showed '/usr/lib/python3.4' and some others. I think it means I have it in my PATH right – Tasdik Rahman Nov 3 '15 at 10:16
  • @cas : I am not sure about that, I was trying to test this with the CGIHTTPServer which comes with python. Any help on that? – Tasdik Rahman Nov 3 '15 at 10:17
  • did you edit those python scripts on a windows machine? do they have CR-LF line-endings rather than just LF? show output of sed -n 1l usr_check.py (that's a 1 and a lower-case l for the sed script) – cas Nov 3 '15 at 10:19

Finally figured out what was wrong with this. Was making a really silly mistake! Thanks to @cas for making me come to it.

Had done a #/usr/bin/env python3.4 instead of #!/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.

That solved the problem for me.

  • should have spotted it myself...didn't even look at the #! lines after the first one. – cas Nov 4 '15 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.