0

I have run into another problem now, with below given code i am unable to login, it still says Access is Denied, even though the username and password is correct .No errors on the console. Looks like i am missing something after the connection.

All i am trying to do here is modify the TODO section so that it runs the computed query against my Oracle database after connecting and a successful login, it should show the results from permissions table.

import cgi
import cx_Oracle

print("Content-type: text/html\n")
print("<title>Test</title>")
print("<body><center>")

try:
    # get post data
    form = cgi.FieldStorage()
    name = form['name'].value if 'name' in form else ''
    pwd = form['pwd'].value if 'pwd' in form else ''

    permissions = []
    # query to check password and get permissions
    query = "SELECT PERMISSIONS FROM USERS WHERE NAME='{}' and PWD='{}'".format(name, pwd)

    # TODO: connect to database and run query
    host = '123.abc.com'
    port = 1521
    SID = 'orcl'
    dsn_tns = cx_Oracle.makedsn(host, port, SID)

    connection = cx_Oracle.connect('abuser', 'userpass', dsn_tns)
    curs = connection.cursor()
    result = curs.execute(query)

    # TODO section ends

    if len(permissions) > 0:
        print("<H1>Access granted. You have the following permissions: {}.</H1>".format(permissions[0][0]))
    else:
        print("<H1>Access denied.</H1>")
    connection.close()    
except cx_Oracle.DatabaseError as e:
    # for ease of debugging
    print("Database Error: {}".format(e))
    print("<br>Query: {}".format(query))

print("""
<form action="../login.html" method="GET">
    <input type="submit" value="Back to Login">
</form>
""")

print('</center></body>')
1
  • I've undone your edit where you deleted the question. Please don't vandalize questions that people have had the courtesy to answer. Commented May 29, 2015 at 0:09

2 Answers 2

1

Your indent is too great starting on line 36 (I guess you uploaded partial source code), where it starts host =

Sign up to request clarification or add additional context in comments.

Comments

0

There is an extra indent in the line starting with host. Indents in Python usually follow the :. The below code should fix the indentation error that you are getting. More about this here - http://www.diveintopython.net/getting_to_know_python/indenting_code.html

import cgi
import cx_Oracle

print("Content-type: text/html\n")
print("<title>Test</title>")
print("<body><center>")

try:
    # get post data
    form = cgi.FieldStorage()
    name = form['name'].value if 'name' in form else ''
    pwd = form['pwd'].value if 'pwd' in form else ''

    permissions = []
    # query to check password and get permissions
    query = "select permissions from users where name='{}' and pwd='{}'".format(name, pwd)

    # Connect to database and run query
    host = '123.abc.com'
    port = 1521
    SID = 'orcl'
    dsn_tns = cx_Oracle.makedsn(host, port, SID)

    connection = cx_Oracle.connect('abcuser', 'abcuserpassword', dsn_tns)
    results = connection.execute(query)

    # TODO section ends

    if len(permissions) > 0:
        print("<H1>Access granted. You have the following permissions: {}.</H1>".format(permissions[0][0]))
    else:
        print("<H1>Access denied.</H1>")
except cx_Oracle.DatabaseError as e:
    # for ease of debugging
    print("Database Error: {}".format(e))
    print("<br>Query: {}".format(query))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.