# a standard 500 Internal Server Error #cgitb.enable() # Read the table to query from HTTP request parameters (in the URL) params = cgi.FieldStorage() table = params.getvalue('t') # Return a 400 error if no table parameter was provided if not table: cgiutils.print_error400( "Invalid request parameters:\n" + "You must provide the table name as a parameter 't'") exit() # Return a 400 error if the provided table does not exist tables = sqlutils.get_tables() if table not in tables: cgiutils.print_error400("The table '" + table + "' does not exist!") exit() # Query for all data and parse into JSON results = sqlutils.exec_readonly_query("SELECT * FROM " + table) results_json = json.dumps(results) # Using the database established a connection # Since we're done with it, we close the connection sqlutils.close() # Print the HTTP response cgiutils.print_response(results_json, "200 OK", "text/json")
from lib import sqlutils # Clear all of the data from a given table # Read the table to query from HTTP request parameters (in the URL) params = cgi.FieldStorage() table = params.getvalue('t') # Return a 400 error if no table parameter was provided if not table: cgiutils.print_error400( "Invalid request parameters:\n" + "You must provide the table name as a parameter 't'") exit() # Return a 400 error if the provided table does not exist tables = sqlutils.get_tables() if table not in tables: cgiutils.print_error400("The table '" + table + "' does not exist!") exit() # Do deletion sqlutils.clear_table(table) # No need to close connection, because when performing volatile operations # (i.e. changing the state of the database), the connection is closed # immediately for security # Print the HTTP response cgiutils.print_response("OK", "200 OK", "text/plain")
# Return a 400 error if no table parameter was provided if not table: cgiutils.print_error400("Invalid request parameters:\n"+ "You must provide a table as a parameter 't'") exit() # Return a 400 error if no type parameter was provided if not insertType: cgiutils.print_error400("Invalid request parameters:\n"+ "You must provide an insertion type as a parameter 'type'") exit() # Return a 400 error if the provided insertion type is invalid if insertType not in ['bulk','single']: cgiutils.print_error400("Invalid insertion type! Must be 'single' or 'bulk'") exit() # Return a 400 error if the provided table does not exist tables = sqlutils.get_tables() if table not in tables: cgiutils.print_error400("The table '"+table+"' does not exist!") exit() # Do insertion and calculate time before = time() sqlutils.insert_data(table,insertType,data) after = time() totalTime = str("%.3f" % (after - before)) # Print the HTTP response with a JSON string specifying the time cgiutils.print_response("{\"time\": \""+totalTime+"\"}", "200 OK", "text/json")
#!/usr/bin/python3 import json import cgi import cgitb from lib import cgiutils from lib import sqlutils # Respond with a list (in JSON format) of the names of all of # the tables in the database # Enabling cgitb will cause a web page to be sent back containing error # information in the event an uncaught error occurs. This is useful for # debugging, but we don't want to expose details about our code to people # visiting the site, so comment it out once you know things are working. # With it disabled, an uncaught error will cause the web server to return # a standard 500 Internal Server Error #cgitb.enable() # Get the list of tables and convert to a JSON string tables = sqlutils.get_tables() tables_json = json.dumps(tables) # Calling get_tables() established a database connection. # Since we're done with it, we close the connection sqlutils.close() # Print the HTTP response cgiutils.print_response(tables_json, "200 OK", "text/json")