/tests/functional/example-backup/main.py
Python | 41 lines | 39 code | 2 blank | 0 comment | 6 complexity | 2bd3ff53c0504cb55aeebf61d8317a04 MD5 | raw file
1import MySQLdb 2import os 3import time 4import traceback 5 6 7def application(environ, start_response): 8 try: 9 import sys 10 print >> sys.stderr, dict(host=os.environ['CONFIG_MYSQL_HOST'] or '127.0.0.1', 11 user=os.environ['CONFIG_MYSQL_USER'], 12 passwd=os.environ['CONFIG_MYSQL_PASSWORD'], 13 db=os.environ['CONFIG_MYSQL_DBNAME'], 14 port=int(os.environ['CONFIG_MYSQL_PORT']) 15 if os.environ['CONFIG_MYSQL_PORT'] else None) 16 kw = {} 17 if os.environ['CONFIG_MYSQL_HOST']: 18 kw['host'] = os.environ['CONFIG_MYSQL_HOST'] 19 if os.environ['CONFIG_MYSQL_PORT']: 20 kw['port'] = int(os.environ['CONFIG_MYSQL_PORT']) 21 if os.environ['CONFIG_MYSQL_PASSWORD']: 22 kw['passwd'] = os.environ['CONFIG_MYSQL_PASSWORD'] 23 conn = MySQLdb.connect(user=os.environ['CONFIG_MYSQL_USER'], 24 db=os.environ['CONFIG_MYSQL_DBNAME'], 25 **kw) 26 cur = conn.cursor() 27 cur.execute(""" 28 CREATE TABLE IF NOT EXISTS test_table ( 29 id INT PRIMARY KEY AUTO_INCREMENT, 30 name TEXT, 31 value TEXT 32 ); 33 """) 34 cur.execute(""" 35 INSERT INTO test_table (name, value) VALUES (%s, %s) 36 """, ('time_run', str(time.time()))) 37 start_response('200 OK', [('content-type', 'text/plain')]) 38 return ['Inserted'] 39 except: 40 start_response('500 Server Error', [('content-type', 'text/plain')]) 41 return [traceback.format_exc()]