Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Created December 22, 2010 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchaplinsky/750885 to your computer and use it in GitHub Desktop.
Save dchaplinsky/750885 to your computer and use it in GitHub Desktop.
Simple test of handlersocket and mysql python working together
#!/usr/bin/env python
from MySQLdb import connect
from pyhs.manager import Manager
from hashlib import md5
from datetime import datetime
DB_NAME = 'simple_test'
DB_USER = 'root'
DB_PASSWORD = ''
TABLE_NAME = 'test_table'
connection = connect(
host = '',
user = DB_USER,
passwd = DB_PASSWORD,
db = DB_NAME,
use_unicode = True
)
cursor = connection.cursor()
cursor.execute('DROP TABLE IF EXISTS %s' % TABLE_NAME)
cursor.execute('CREATE TABLE `%s` (\
`cache_key` varchar(255) NOT NULL PRIMARY KEY,\
`value` longtext NOT NULL,\
`expires` datetime NOT NULL\
)' % TABLE_NAME)
manager = Manager( [('inet', 'localhost', 9998)],
[('inet', 'localhost', 9999)])
for i in range(0, 10):
manager.insert(
DB_NAME,
TABLE_NAME,
[ ('cache_key', md5(str(i)).hexdigest()),
('value', md5(str(i) + 'foobar').hexdigest()),
('expires', str(datetime.now()))],
)
cursor.execute("SELECT count(*) FROM `%s`" % TABLE_NAME)
print cursor.fetchone()
# connection.rollback()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment