-
-
Save jeremyvisser/4358965 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cgi | |
import cgitb | |
import mysql.connector | |
DB_HOST = 'localhost' | |
DB_DATABASE = '' | |
DB_USER = '' | |
DB_PASS = '' | |
cgitb.enable(display=1) | |
class Blog: | |
def __init__(self, db): | |
self.db = db | |
def header(self): | |
print 'Content-type: text/html;charset=utf-8' | |
print '' | |
print '<!doctype html>' | |
print '<title>My Blog</title>' | |
def show_post(self, id): | |
query = ('SELECT ID, post_date, post_title, post_content FROM wp_posts' | |
' WHERE ID = %s and post_status = \'published\'') | |
cur = self.db.cursor() | |
cur.execute(query, (id)) | |
for row in cur: | |
print '<h2>%s</h2>' % row[2].encode('utf-8') | |
print '%s' % row[3].encode('utf-8') | |
def show_all_posts(self): | |
query = ('SELECT ID, post_date, post_title FROM wp_posts' | |
' WHERE post_type = \'post\'' | |
' ORDER BY post_date DESC LIMIT 10') | |
cur = self.db.cursor() | |
cur.execute(query) | |
print '<ul>' | |
for row in cur: | |
print '<li><a href="?id=%d">%s</li>' % (row[0], row[2].encode('utf-8')) | |
print '</ul>' | |
self.db.commit() | |
if __name__=='__main__': | |
db = mysql.connector.connect(host=DB_HOST, user=DB_USER, password=DB_PASS, database=DB_DATABASE) | |
blog = Blog(db) | |
blog.header() | |
args = cgi.parse() | |
if 'id' in args: | |
blog.show_post(args['id']) | |
else: | |
blog.show_all_posts() | |
db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment