achilles_aux
achilles_aux

Reputation: 15

Issues with python script


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

Upvotes: 0

Views: 264

Answers (2)

fixxxer
fixxxer

Reputation: 16154

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

Upvotes: 0

Christopher Horler
Christopher Horler

Reputation: 73

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

Upvotes: 1

Related Questions