#!/usr/local/bin/python3 from cgitb import enable enable() from cgi import FieldStorage from html import escape import pymysql as db print('Content-Type: text/html') print() result = '' try: connection = db.connect('localhost', 'userid', 'password', 'database_name') cursor = connection.cursor(db.cursors.DictCursor) cursor.execute("""SELECT candidate_name, total_votes FROM candidates ORDER BY total_votes DESC, candidate_name ASC""") result = '' for row in cursor.fetchall(): result += '' % (row['candidate_name'], row['total_votes']) result += '
Candidate nameTotal votes
%s%i
' cursor.close() connection.close() except db.Error: result = '

Sorry! We are experiencing problems at the moment. Please call back later.

' print(""" Leaderboard %s """ % (result))