Not getting get_secret_value_response from client.get_secret_value() - python

I am trying to get secret but i am getting exception - 'NoneType' object is not subscriptable as I'm not getting secret from get_secret_value.
Below are my codes:
class OracleConnection:
"""
This is a helper class for making authenticated connections to Oracle.
"""
def init(self):
self.evars = os.environ
print("1")
self.secret_name = self.evars.get("ORACLE_SECRET_ARN", "cci-acoe-secrets-dss-oracle-uet-dev")
self.region_name = self.evars.get("REGION", "us-east-1")
self.encoding = 'UTF-8'
self.connection = None
self.pool = None
def get_secret(self):
global secret
print("2")
session = boto3.session.Session()
print("3")
client = session.client(
config=config,
service_name='secretsmanager',
region_name=self.region_name
)
print("4")
try:
print(self.secret_name)
get_secret_value_response = client.get_secret_value(
SecretId=self.secret_name
)
print(get_secret_value_response)
print("5")
except ClientError as e:
if e.response['Error']['Code'] == 'DecryptionFailureException':
raise e
elif e.response['Error']['Code'] == 'InternalServiceErrorException':
raise e
elif e.response['Error']['Code'] == 'InvalidParameterException':
raise e
elif e.response['Error']['Code'] == 'InvalidRequestException':
raise e
elif e.response['Error']['Code'] == 'ResourceNotFoundException':
raise e
else:
print("6")
if 'SecretString' in get_secret_value_response:
secret_val = get_secret_value_response['SecretString']
else:
secret_val = base64.b64decode(get_secret_value_response['SecretBinary'])
secret = json.loads(secret_val)
def __enter__(self):
"""
This method opens the Oracle connection.
"""
logger.debug("OracleConnection initializing connection with Oracle.")
logger.debug("Gathering Oracle credentials.")
try:
print("7")
self.get_secret()
print(secret)
info = {
'user': secret['username'],
'password': secret['password'],
'host': secret['host'],
'service': secret['dbname'],
'port': int(secret['port'])
}
# Make DSN address
self.user = info.get('user')
self.pwd = info.get('password')
self.host = info.get('host')
self.port = info.get('port')
self.service = info.get('service')
self.dsn = cx_Oracle.makedsn(self.host, self.port, service_name=self.service)
# Create the session pool
self.pool = cx_Oracle.SessionPool(
self.user,
self.pwd,
self.dsn,
min=100,
max=100,
increment=0,
encoding=self.encoding
)
# Acquire a connection from the pool
self.connection = self.pool.acquire()
print(self.connection.version)
except cx_Oracle.Error as error:
logger.critical("Failed to establish Oracle connection.\nError: {}".format(error))
raise
logger.debug("Opening Oracle Connection.")
return self
It's printing 1 2 3 4 also secret name but don't know why i am not getting any response from client.get_secret_value()? Please someone help.

I was having the same problem. Is your lambda attached to a VPC? If so, make sure you choose public subnets for it. It has something to do with private subnets cannot connect to the internet...and your secrets manager call will not work. I am also figuring this out.

Related

Slack token verification returning two seperate signatures?

I'm trying to validate my Slack bot using the example code provided in the Slack documentation here. However, it is returning two different signatures.
def lambda_handler(event, context):
# get slack secret from secrets manager
secret = get_secret()
# needed for creating hmac
qstring = base64.b64decode(event['body']).decode('utf-8')
return validate_request(secret, qstring, event['headers'])
# get slack secret for verification from secrets manager
def get_secret():
secret_name = "<omitted>"
region_name = "<omitted>"
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
if e.response['Error']['Code'] == 'DecryptionFailureException':
raise e
elif e.response['Error']['Code'] == 'InternalServiceErrorException':
raise e
elif e.response['Error']['Code'] == 'InvalidParameterException':
raise e
elif e.response['Error']['Code'] == 'InvalidRequestException':
raise e
elif e.response['Error']['Code'] == 'ResourceNotFoundException':
raise e
else:
if 'SecretString' in get_secret_value_response:
return get_secret_value_response['SecretString']
else:
return base64.b64decode(get_secret_value_response['SecretBinary'])
# validate bot request
def validate_request(secret, body, headers):
timestamp = headers['x-slack-request-timestamp']
sig_basestring = 'v0:' + timestamp + ':' + body
my_signature = 'v0=' + hmac.new(secret.encode('utf_8'), sig_basestring.encode('utf_8'), hashlib.sha256).hexdigest()
slack_signature = headers['x-slack-signature']
if hmac.compare_digest(my_signature, slack_signature):
return True
else:
return False
Result:
v0=24dde133843073b58084970afe027e3a4dabc1b8d9efc5248a97ad64c6529cee
v0=bf51d6fb9eb56d5c6ea19e866b798903fb0cee67264cb467ee7924bb13571770
Any ideas why? I've verified that the correct token is returned by get_secret() and that the qstring variable contains the correct query parameters as shown in Slack's documentation. The headers all contain the correct values too.
For me the signatures were different because I was formatting the body received from slack request to json before passing to the verify function.
Passing the body as it is resolved my issue.
Format of body to be passed to verify function-
'body': 'token=XXXXXXXXXX&team_id=XXXXXXX&team_domain=XXXXXX&channel_id=XXXXX&channel_name=XXXXXX&user_id=XXX&user_name=XXXX&command=XXXXX&text=XXXX&api_app_id=XXXXXX&is_enterprise_install=false&response_url=XXXXX&trigger_id=XXXXXXXX'

Pyodbc if connection fails retry

Hi there I have the following python code to connect to my SQL-Server DB
class CDBTools:
details = {
'server' : 'localhost',
'database' : 'MyDB',
'username' : 'me',
'password' : 'myPass'
}
conn = None
def __init__(self, server, database, username, password):
self.details["server"] = server
self.details["database"] = database
self.details["username"] = username
self.details["password"] = password
def connect_to_db(self):
connect_string = 'DRIVER={{FreeTDS}};SERVER={server}; DATABASE={database};UID={username};PWD={password}'.format(**self.details)
try:
self.conn = pyodbc.connect(connect_string, autocommit=True)
#print(connect_string)
#Logger.Log(self.conn, "info")
except pyodbc.Error as e:
print(e, "error")
def execute_select_query(self, query):
try:
curr = self.conn.cursor()
out = curr.execute(query).fetchall()
except pyodbc.IntegrityError as e:
out = []
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
out = []
print('ms-sql Operation Error: {0}'.format(err))
except AttributeError as err:
out = []
print('Connection to DB failed')
pass
try:
curr.close()
except:
print('Connection to DB failed')
return out
def execute_inset_query(self, query):
try:
database_cursor = self.conn.cursor()
database_cursor.execute(query)
except pyodbc.DataError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.IntegrityError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
print('ms-sql error: {0}'.format(e))
then in my main program I am trying this and it works just fine, until I disconnect the network
DBT = CDBTools("192.168.1.2\instance4", "my_db", "my_username", "my_passowrd")
DBT.connect_to_db()
while(True):
print("[{0}]: {1}".format(time.strftime("%H:%M:%S"), DBT.execute_select_query("SELECT Name FROM Persons WHERE ID='1'")))
When I disconnect the network I get no error, just time doesn't count anymore (of course because query is failing) but when I reconnect the network query never sucseeds anymore
so does anyone maybe know how I can modify execute_select_query and execute_inset_query so when connection to the db is restored it will start to work again :)
Thanks for Anwsering and Best Regards
Try this, it'll connect each time you use the with clause, and automatically disconnect when you leave it.
class CDBTools:
details = {
'server' : 'localhost',
'database' : 'MyDB',
'username' : 'me',
'password' : 'myPass'
}
conn = None
def __init__(self, server, database, username, password):
self.details["server"] = server
self.details["database"] = database
self.details["username"] = username
self.details["password"] = password
def connect_to_db(self):
connect_string = 'DRIVER={{FreeTDS}};SERVER={server}; DATABASE={database};UID={username};PWD={password}'.format(**self.details)
try:
conn = pyodbc.connect(connect_string, autocommit=True)
#print(connect_string)
#Logger.Log(self.conn, "info")
except pyodbc.Error as e:
print(e, "error")
return conn
def execute_select_query(self, conn, query):
try:
curr = conn.cursor()
out = curr.execute(query).fetchall()
except pyodbc.IntegrityError as e:
out = []
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
out = []
print('ms-sql Operation Error: {0}'.format(err))
except AttributeError as err:
out = []
print('Connection to DB failed')
pass
def execute_inset_query(self, conn, query):
try:
database_cursor = conn.cursor()
database_cursor.execute(query)
except pyodbc.DataError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.IntegrityError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
print('ms-sql error: {0}'.format(e))
then:
DBT = CDBTools("192.168.1.2\instance4", "my_db", "my_username", "my_passowrd")
while True:
with DBT.connect_to_db() as conn:
print("[{0}]: {1}".format(time.strftime("%H:%M:%S"), DBT.execute_select_query(conn, "SELECT Name FROM Persons WHERE ID='1'")))
I'd probably make a method to return the cursor rather than the connection. For example:
class CDBTools:
details = {
'server' : 'localhost',
'database' : 'MyDB',
'username' : 'me',
'password' : 'myPass'
}
conn = None
def __init__(self, server, database, username, password):
self.details["server"] = server
self.details["database"] = database
self.details["username"] = username
self.details["password"] = password
def get_cursor(self):
connect_string = 'DRIVER={{FreeTDS}};SERVER={server}; DATABASE={database};UID={username};PWD={password}'.format(**self.details)
try:
conn = pyodbc.connect(connect_string, autocommit=True)
#print(connect_string)
#Logger.Log(self.conn, "info")
except pyodbc.Error as e:
print(e, "error")
return conn.cursor()
DBT = CDBTools("192.168.1.2\instance4", "my_db", "my_username", "my_passowrd")
with DBT.get_cursor() as cursor:
cursor.execute("SELECT Name FROM Persons WHERE ID='1'")
for row in cursor.fetchall():
print(row)
Good luck!

How to mock functionality of boto3 module using pytest

I have a custom module written called sqs.py. The script will do the following:
Get a message from AWS SQS
Get the AWS S3 path to delete
Delete the path
Send a confirmation email to the user
I'm trying to write unit tests for this module that will verify the code will execute as expected and that it will raise exceptions when they do occur.
This means I will need to mock the response from Boto3 calls that I make. My problem is that the code will first establish the SQS client to obtain the message and then a second call to establish the S3 client. I'm not sure how to mock these 2 independent calls and be able to fake a response so I can test my script's functionality. Perhaps my approach is incorrect. At any case, any advice on how to do this properly is appreciated.
Here's how the code looks like:
import boto3
import json
import os
import pprint
import time
import asyncio
import logging
from send_email import send_email
queue_url = 'https://xxxx.queue.amazonaws.com/1234567890/queue'
def shutdown(message):
""" Sends shutdown command to OS """
os.system(f'shutdown +5 "{message}"')
def send_failure_email(email_config: dict, error_message: str):
""" Sends email notification to user with error message attached. """
recipient_name = email_config['recipient_name']
email_config['subject'] = 'Subject: Restore Failed'
email_config['message'] = f'Hello {recipient_name},\n\n' \
+ 'We regret that an error has occurred during the restore process. ' \
+ 'Please try again in a few minutes.\n\n' \
+ f'Error: {error_message}.\n\n' \
try:
send_email(email_config)
except RuntimeError as error_message:
logging.error(f'ERROR: cannot send email to user. {error_message}')
async def restore_s3_objects(s3_client: object, p_bucket_name: str, p_prefix: str):
"""Attempts to restore objects specified by p_bucket_name and p_prefix.
Returns True if restore took place, false otherwise.
"""
is_truncated = True
key_marker = None
key = ''
number_of_items_restored = 0
has_restore_occured = False
logging.info(f'performing restore for {p_bucket_name}/{p_prefix}')
try:
while is_truncated == True:
if not key_marker:
version_list = s3_client.list_object_versions(
Bucket = p_bucket_name,
Prefix = p_prefix)
else:
version_list = s3_client.list_object_versions(
Bucket = p_bucket_name,
Prefix = p_prefix,
KeyMarker = key_marker)
if 'DeleteMarkers' in version_list:
logging.info('found delete markers')
delete_markers = version_list['DeleteMarkers']
for d in delete_markers:
if d['IsLatest'] == True:
key = d['Key']
version_id = d['VersionId']
s3_client.delete_object(
Bucket = p_bucket_name,
Key = key,
VersionId = version_id
)
number_of_items_restored = number_of_items_restored + 1
is_truncated = version_list['IsTruncated']
logging.info(f'is_truncated: {is_truncated}')
if 'NextKeyMarker' in version_list:
key_marker = version_list['NextKeyMarker']
if number_of_items_restored > 0:
has_restore_occured = True
return has_restore_occured
except Exception as error_message:
raise RuntimeError(error_message)
async def main():
if 'AWS_ACCESS_KEY_ID' in os.environ \
and 'AWS_SECRET_ACCESS_KEY' in os.environ \
and os.environ['AWS_ACCESS_KEY_ID'] != '' \
and os.environ['AWS_SECRET_ACCESS_KEY'] != '':
sqs_client = boto3.client(
'sqs',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
verify=False
)
s3_client = boto3.client(
's3',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
verify=False
)
else:
sqs_client = boto3.client(
'sqs',
verify=False,
)
s3_client = boto3.client(
's3',
verify=False,
)
received_message = sqs_client.receive_message(
QueueUrl=queue_url,
AttributeNames=['All'],
VisibilityTimeout=10,
WaitTimeSeconds=20, # Wait up to 20 seconds for a message to arrive
)
if 'Messages' in received_message \
and len(received_message['Messages']) > 0:
# NOTE: Initialize email configuration
receipient_email = 'support#example.com'
username = receipient_email.split('#')[0]
fullname_length = len(username.split('.'))
fullname = f"{username.split('.')[0]}" # Group name / First name only
if (fullname_length == 2): # First name and last name available
fullname = f"{username.split('.')[0]} {username.split('.')[1]}"
fullname = fullname.title()
email_config = {
'destination': receipient_email,
'recipient_name': fullname,
'subject': 'Subject: Restore Complete',
'message': ''
}
try:
receipt_handle = received_message['Messages'][0]['ReceiptHandle']
except Exception as error_message:
logging.error(error_message)
send_failure_email(email_config, error_message)
shutdown(f'{error_message}')
try:
data = received_message['Messages'][0]['Body']
data = json.loads(data)
logging.info('A SQS message for a restore has been received.')
except Exception as error_message:
message = f'Unable to obtain and parse message body. {error_message}'
logging.error(message)
send_failure_email(email_config, message)
shutdown(f'{error_message}')
try:
bucket = data['bucket']
prefix = data['prefix']
except Exception as error_message:
message = f'Retrieving bucket name and prefix failed. {error_message}'
logging.error(message)
send_failure_email(email_config, message)
shutdown(f'{error_message}')
try:
logging.info(f'Initiating restore for path: {bucket}/{prefix}')
restore_was_performed = await asyncio.create_task(restore_s3_objects(s3_client, bucket, prefix))
if restore_was_performed is True:
email_config['message'] = f'Hello {fullname},\n\n' \
+ f'The files in the path \'{bucket}/{prefix}\' have been restored. ' \
send_email(email_config)
logging.info('Restore complete. Shutting down.')
else:
logging.info('Path does not require restore. Shutting down.')
shutdown(f'shutdown +5 "Restore successful! System will shutdown in 5 mins"')
except Exception as error_message:
message = f'File restoration failed. {error_message}'
logging.error(message)
send_failure_email(email_config, message)
shutdown(f'{error_message}')
try:
sqs_client.delete_message(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle,
)
except Exception as error_message:
message = f'Deleting restore session from SQS failed. {error_message}'
logging.error(message)
send_failure_email(email_config, message)
shutdown(f'{error_message}')
if __name__ == '__main__':
logging.basicConfig(filename='restore.log',level=logging.INFO)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
The only way I was able to mock Boto3 is rebuilding a small class that represents the actual method structure. This is because Boto3 uses dynamic methods and all the resource level methods are created at runtime.
This might not be industry standard but I wasn't able to get any of the methods I found on the internet to work most of the time and this worked pretty well for me and requires minimal effort (comparing to some of the solutions I found).
class MockClient:
def __init__(self, region_name, aws_access_key_id, aws_secret_access_key):
self.region_name = region_name
self.aws_access_key_id = aws_access_key_id
self.aws_secret_access_key = aws_secret_access_key
self.MockS3 = MockS3()
def client(self, service_name, **kwargs):
return self.MockS3
class MockS3:
def __init__(self):
self.response = None # Test your mock data from S3 here
def list_object_versions(self, **kwargs):
return self.response
class S3TestCase(unittest.TestCase):
def test_restore_s3_objects(self):
# Given
bucket = "testBucket" # Test this to something that somewahat realistic
prefix = "some/prefix" # Test this to something that somewahat realistic
env_vars = mock.patch.dict(os.environ, {"AWS_ACCESS_KEY_ID": "abc",
"AWS_SECRET_ACCESS_KEY": "def"})
env_vars.start()
# initialising the Session can be tricy since it has to be imported from
# the module/file that creates the session on actual code rather than
# where's a Session code is. In this case you might have to import from
# main rather than boto3.
boto3.session.Session = mock.Mock(side_effect=[
MockClient(region_name='eu-west-1',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'])])
s3_client = boto3.client('s3', verify=False)
# When
has_restore_occured = restore_s3_objects(s3_client, bucket, prefix)
# Then
self.assertEqual(has_restore_occured, False) # your expected result set
env_vars.stop()

Sockets Programming with Video Streaming with python

I have to make a project where i accept multiple client connections and I have to send screenshots of the screen of the client to the server and show it on the screen and if I have to be able to stream clients screen to the server. How do I go about this? I've been trying to make this for about 2 months now. I am stuck and don't know how to make it.
Any reply is greatly appreciated.
This is the server code I have so far.
import threading
import socket
from queue import Queue
NUMBER_OF_THREADS = 2
JOB_NUMBER = [1,2]
queue = Queue()
class MultiServer(object):
#Initialize Server Object with host, port, socket, all connections and all the IP addresses
def __init__(self):
self.host = ""
self.port = 8965
self.socket = None
self.all_connections = []
self.all_addresses = []
self.all_users = []
#Create Socket
def socket_create(self):
#If socket creation fails except the error and show to the User and exit the program
try:
self.socket = socket.socket()
except socket.error as msg:
print("Socket creation failed, error: " +str(msg))
sys.exit(1)
#Use socket.setsockopt to keep reusing the same port
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return
def socket_bind(self):
#Bind socket to port and wait for incoming connections
try:
self.socket.bind((self.host, self.port))
self.socket.listen(5)
#Is binding the socket fails show error to the user and try again in 5 seconds
except socket.error as msg:
print("Socket binding error: " + str(msg))
time.sleep(5)
self.socket_bind()
return
def accept_connections(self):
#Accept Connections from multiple clients and save to list
#Close all old connections to prevent errors
for c in self.all_connections:
c.close()
self.all_connections = []
self.all_addresses = []
while True:
try:
conn, address = self.socket.accept()
conn.setblocking(1)
client_hostname = conn.recv(1024).decode("utf-8")
address= address + (client_hostname,)
except Exception as ex:
print('Error accepting connections: %s' %str(ex))
# Inifinite Loop
continue
self.all_connections.append(conn)
self.all_addresses.append(address)
print('\nConnection has been estalished: {0} ({1})'.format(address[-1], address[0]))
return
def send_commands(self):
while True:
cmd = input("$:")
if cmd == "list":
self.list_connections()
def list_connections(self):
""" List all connections """
results = ''
for i, conn in enumerate(self.all_connections):
try:
conn.send(str.encode(' '))
conn.recv(20480)
except:
del self.all_connections[i]
del self.all_addresses[i]
continue
results += str(i) + ' ' + str(self.all_addresses[i][0]) + ' ' + str(
self.all_addresses[i][1]) + ' ' + str(self.all_addresses[i][2]) + '\n'
print('----- Clients -----' + '\n' + results)
return
def create_workers():
server = MultiServer()
for i in range(2):
t = threading.Thread(target=work, args=(server,))
t.daemon = True
t.start()
def work(server):
while True:
x = queue.get()
if x == 1:
server.socket_create()
server.socket_bind()
server.accept_connections()
if x == 2:
server.send_commands()
queue.task_done()
return
def create_jobs():
for x in JOB_NUMBER:
queue.put(x)
queue.join()
def main():
create_workers()
create_jobs()
if __name__ == '__main__':
main()
And this is the Client code I have so far
import socket
class Client(object):
def __init__(self):
self.serverHost = '127.0.0.1'
self.serverPort = 8965
self.socket = None
def socket_create(self):
""" Create a socket """
try:
self.socket = socket.socket()
except socket.error as e:
print("Socket creation error" + str(e))
return
return
def socket_connect(self):
""" Connect to a remote socket """
try:
self.socket.connect((self.serverHost, self.serverPort))
except socket.error as e:
print("Socket connection error: " + str(e))
time.sleep(5)
raise
try:
self.socket.send(str.encode(socket.gethostname()))
except socket.error as e:
print("Cannot send hostname to server: " + str(e))
raise
return
def recieve_requests(self):
""" Controle of de server nog actief is """
try:
self.socket.recv(10)
except Exception as ex:
print("Couldn't recieve Commands: " + str(ex))
return
while True:
data = self.socket.recv(20480).decode("utf-8")
print(data)
print("mislukt")
client = Client()
client.socket_create()
def main():
client = Client()
client.socket_create()
try:
client.socket_connect()
except Exception as ex:
print(f"Socket connection error: {ex}")
while True:
try:
client.recieve_requests()
except Exception as ex:
print(ex)

Python and Flask local variable 'cursor' referenced before assignment

sorry for my english google translator.
I'm learning python and flask and I have a problem in a function.
#app.route('/addProperty',methods=['POST'])
def addProperty():
try:
if session.get('user'):
_tag = request.form['inputTag']
_idCategoria = request.form['inputIdCategoria']
_descricaoBem = request.form['inputDescricaoBem']
_valor = request.form['inputValor']
_fornecedor = request.form['inputFornecedor']
_nfNumero = resquest.form['inputNfNumero']
_nfSerie = request.form['inputNfSerie']
_dtCompra = resquest.form['inputDtCompra']
_departamento = request.form['inputDepartamento']
_local = request.form['inputLocal']
_responsavel = resquest.form['inputResponsavel']
_estadoBem = request.form['inputEstadoBem']
_dtUltimaRev = request.form['inputDtUltimaRev']
_dtProximaRev = request.form['inputDtProximaRev']
_idade = request.form['inputIdade']
_vidaUtil = request.form['inputVidaUtil']
_trocarReformar = request.form['inputTrocaReforma']
_valorTT = request.form['inputValorTT']
curDb.callproc('sp_addProperty',(_tag,_idCategoria,_descricaoBem,_valor,_fornecedor,
_nfNumero,_nfSerie,_dtCompra,_departamento,_local,
_responsavel,_estadoBem,_dtUltimaRev,_dtProximaRev,
_idade,_vidaUtil,_trocarReformar,_valorTT))
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_addProperty',(_departamento,_user))
data = cursor.fetchall()
if len(data) is 0:
conectaDb.commit()
return redirect('/userHome')
else:
return render_template('error.html',error = 'An error occurred!')
else:
return render_template('error.html',error = 'Unauthorized Access')
except Exception as e:
return render_template('error.html',error = str(e))
finally:
cursor.close()
conn.close()
This function is returning the following error:
UnboundLocalError: local variable 'cursor' referenced before assignment
And the function below is the same structure and does not return error.
#app.route('/addStatus',methods=['POST'])
def addStatus():
try:
if session.get('user'):
_status = request.form['inputStatus']
_user = session.get('user')
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_addStatus',(_status,_user))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return redirect('/userHome')
else:
return render_template('error.html',error = 'An error occurred!')
else:
return render_template('error.html',error = 'Unauthorized Access')
except Exception as e:
return render_template('error.html',error = str(e))
finally:
cursor.close()
conn.close()
I would be very grateful if someone can help me with this problem.
I was assigning the parameter request.form the aliases incorrectly.
Some aliases was getting request.form where the right would request.form

Categories

Resources