-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5f47451
Showing
84 changed files
with
17,053 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
|
||
import os | ||
import torndb | ||
import tornado.httpserver | ||
import tornado.ioloop | ||
import tornado.options | ||
import tornado.web | ||
|
||
#setting | ||
from tornado.options import define, options | ||
|
||
define("port", defaut=8888,help="run port",type=int) | ||
define("mysql_host", default="127.0.0.1:3306", help="db host") | ||
define("mysql_database", default="todo", help="db name") | ||
define("mysql_user", default="root", help="db user") | ||
define("mysql_password", default="", help="db password") | ||
|
||
TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "templates") | ||
STATIC_PATH = os.path.join(os.path.dirname(__file__), "static") | ||
|
||
class Application(tornado.web.Application): | ||
def __init__(self): | ||
''' | ||
handlers = [ | ||
(r"/", IndexHandler), | ||
(r"/account/register", RegisterHandler) | ||
(r"/account/login", Login) | ||
] | ||
''' | ||
handlers =[ | ||
(r"/", IndexHandler), | ||
] | ||
settings = dict( | ||
template_path = TEMPLATE_PATH, | ||
static_path = STATIC_PATH, | ||
debug = True | ||
) | ||
tornado.web.Application.__init__(self, handlers, **settings) | ||
self.db = torndb.Connection( | ||
host = options.mysql_host, | ||
database = options.mysql_database, | ||
user = options.mysql_user, | ||
password = options.mysql_password | ||
) | ||
def main(): | ||
tornado.options.parse_command_line() | ||
app = tornado.httpserver.HTTPServer(Application()) | ||
app.listen(options.port) | ||
tornado.ioloop.IOLoop.instance().start() | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import sys | ||
import logging | ||
import logging.handlers | ||
|
||
def config_logger(): | ||
"""配置logging的日志文件以及日志的记录等级 | ||
""" | ||
|
||
log = logging.getLogger("aixie") | ||
log.setLevel(logging.DEBUG) | ||
file_handler = logging.handlers.TimedRotatingFileHandler('yixie-time.log', when='H', interval=1, backupCount=40) | ||
file_handler.setLevel(logging.INFO) | ||
# 2014-07-06 23:47:34 | ||
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s", | ||
'%Y-%m-%d %H:%M:%S') | ||
file_handler.setFormatter(formatter) | ||
log.addHandler(file_handler) | ||
""" | ||
debug_handler = logging.FileHandler("etdown-admin.log") | ||
debug_handler.setLevel(logging.ERROR) | ||
#debug_handler.setLevel(logging.DEBUG) | ||
debug_formatter = logging.Formatter("%(asctime)s %(filename)s[line:%(lineno)d %(funcName)s] %(levelname)s [%(threadName)s] %(message)s", '%Y-%m-%d %H:%M:%S') | ||
debug_handler.setFormatter(debug_formatter) | ||
log.addHandler(debug_handler) | ||
""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
|
||
import tornado | ||
import simplejson | ||
|
||
|
Empty file.
Empty file.
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
from pymongo.connection import MongoClient | ||
import traceback | ||
from aixie.settings | ||
|
||
class BasePipeline(object): | ||
"""save the data to mongodb. | ||
""" | ||
def __init__(self): | ||
"""The only async framework that PyMongo fully supports is Gevent. | ||
Currently there is no great way to use PyMongo in conjunction with | ||
Tornado or Twisted. PyMongo provides built-in connection pooling, so | ||
some of the benefits of those frameworks can be achieved just by | ||
writing multi-threaded code that shares a MongoClient. | ||
""" | ||
|
||
try: | ||
client = MongoClient(settings['MONGODB_SERVER'], | ||
settings['MONGODB_PORT']) | ||
self.db = client[settings['MONGODB_DB']] | ||
self.coll = self.db[settings['MONGODB_DB_COLL']] | ||
except Exception as e: | ||
log.msg("connect to mongodb{%s:%d %s %s} error. %s" % | ||
(settings['MONGODB_SERVER'], settings['MONGODB_PORT'], | ||
settings['MONGODB_DB'], settings['MONGODB_DB_COLL'], | ||
traceback.print_exc()), level=log.ERROR, spider=None) | ||
|
||
def count(self, query={}): | ||
if type(query) is not dict: | ||
return None | ||
|
||
return self.coll.find(query).count() | ||
|
||
def find(self, query={}): | ||
if type(query) is not dict: | ||
return None | ||
|
||
return self.coll.find(query) | ||
|
||
def find_data(self, query={}, data={}): | ||
if type(query) is not dict or type(data) is not dict: | ||
return None | ||
|
||
return self.coll.find(query, data) | ||
|
||
def insert(self, data): | ||
if type(data) is not dict: | ||
return | ||
|
||
result = self.coll.save(data) | ||
|
||
def remove(self, data={}): | ||
if type(data) is not dict: | ||
return | ||
|
||
result = self.coll.remove(data) | ||
|
||
def update_inc(self, data, incdata): | ||
if type(data) is not dict or type(incdata) is not dict: | ||
return | ||
|
||
self.coll.update(data, {'$inc': incdata}) | ||
|
||
def update_set(self, data, setdata): | ||
if type(data) is not dict or type(setdata) is not dict: | ||
return | ||
|
||
self.coll.update(data, {'$set': setdata}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
doc:http://mysql-python.sourceforge.net/MySQLdb.html | ||
""" | ||
|
||
import MySQLdb | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
class MySQLDB(object): | ||
def __init__(self, host, user, passwd, charset="utf8"): | ||
self.host = host | ||
self.user = user | ||
self.passwd = passwd | ||
self.charset = charset | ||
self._connect() | ||
|
||
def _connect(self): | ||
try: | ||
# 连接设置 | ||
self.conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.passwd) | ||
# 设置编码 | ||
self.conn.set_character_set(self.charset) | ||
# 获取游标 | ||
self.cur = self.conn.cursor() | ||
except MySQLdb.Error, e: | ||
log.error("Mysql error %d: %s" % (e.args[0], e.args[1])) | ||
|
||
# 选择数据库 | ||
def select_db(self, db_name): | ||
try: | ||
self.db_name = db_name | ||
self.conn.select_db(db_name) | ||
except MySQLdb.Error, e: | ||
log.error("Mysql error %d: %s" % (e.args[0], e.args[1])) | ||
|
||
def query(self, sql, *params): | ||
"""#how to correctly do secured & scaped queries | ||
#http://stackoverflow.com/a/1307413 | ||
""" | ||
try: | ||
# 执行语句 | ||
ret = self.cur.execute(sql, *params) | ||
return ret | ||
except (AttributeError, MySQLdb.OperationalError): | ||
log.info("mysql reconnect"); | ||
# 2014 Error – Commands out of sync | ||
# http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/ | ||
#self.close() | ||
# error 2013 lost connection to mysql server during query | ||
self._connect() | ||
self.select_db(self.db_name) | ||
# 执行语句 | ||
ret = self.cur.execute(sql, *params) | ||
return ret | ||
except MySQLdb.Error, e: | ||
log.error("Mysql error %d: %s" % (e.args[0], e.args[1])) | ||
|
||
def query_row(self, sql): | ||
self.query(sql) | ||
ret = self.cur.fetchone() | ||
return ret[0] | ||
|
||
def query_all(self, sql): | ||
self.query(sql) | ||
ret = self.cur.fetchall() | ||
desc = self.cur.description | ||
d = [] | ||
|
||
for inv in ret: | ||
_d = {} | ||
for i in range(0, len(inv)): | ||
_d[desc[i][0]] = str(inv[i]) | ||
|
||
d.append(_d) | ||
return d | ||
|
||
def insert(self, table_name, data): | ||
for key in data: | ||
data[key] = "'" + str(data[key]) + "'" | ||
|
||
key = ','.join(data.keys()) | ||
value = ','.join(data.values()) | ||
|
||
sql = "INSERT INTO " + table_name + " (" + key + ") VALUES (" + value + ")" | ||
|
||
return self.query(sql) | ||
|
||
# 提交事务,否则不能真正的插入数据 | ||
def commit(self): | ||
self.conn.commit() | ||
|
||
# 事务回滚 | ||
def rollback(self): | ||
self.conn.rollback() | ||
|
||
# 关闭数据库连接,释放资源 | ||
def close(self): | ||
self.cur.close() | ||
self.conn.close() | ||
|
||
from MySQLdb.cursors import DictCursor | ||
from DBUtils.PooledDB import PooledDB | ||
|
||
class MySQLDB_(object): | ||
"""MySQL 数据库对象,负责产生数据库连接 , 此类中的连接采用连接池实现 | ||
获取连接对象: conn = MysqlPool._connect() | ||
释放连接对象: conn.close() | ||
http://my.oschina.net/zhouguanghu/blog/32422 | ||
http://www.dbafree.net/?p=1125 | ||
""" | ||
# 连接池对象 | ||
pool = None | ||
def __init__(self, host, user, passwd, db, charset="utf8"): | ||
"""数据库构造函数,从连接池中取出连接,并生成操作游标 | ||
""" | ||
self.conn = MySQLDB_._connect(host, user, passwd, charset, db) | ||
self.cur = self.conn.cursor() | ||
|
||
@staticmethod | ||
def _connect(host, user, passwd, charset, db): | ||
"""静态方法,从连接池中取出连接 | ||
""" | ||
if MySQLDB_.pool is None: | ||
pool = PooledDB(MySQLdb, host=host, user=user, passwd=passwd, | ||
charset=charset, port=3306, db=db, mincached=1, | ||
maxcached=20, maxshared=2, maxconnections=2) | ||
return pool.connection() | ||
|
||
def query(self, sql, *params): | ||
"""#how to correctly do secured & scaped queries | ||
#http://stackoverflow.com/a/1307413 | ||
""" | ||
try: | ||
# 执行语句 | ||
ret = self.cur.execute(sql, *params) | ||
return ret | ||
except (AttributeError, MySQLdb.OperationalError): | ||
log.error("mysql lose connection"); | ||
except MySQLdb.Error, e: | ||
log.error("Mysql error %d: %s" % (e.args[0], e.args[1])) | ||
|
||
def query_row(self, sql): | ||
self.query(sql) | ||
ret = self.cur.fetchone() | ||
return ret[0] | ||
|
||
def query_all(self, sql): | ||
self.query(sql) | ||
ret = self.cur.fetchall() | ||
desc = self.cur.description | ||
d = [] | ||
|
||
for inv in ret: | ||
_d = {} | ||
for i in range(0, len(inv)): | ||
_d[desc[i][0]] = str(inv[i]) | ||
|
||
d.append(_d) | ||
return d | ||
|
||
def insert(self, table_name, data): | ||
for key in data: | ||
data[key] = "'" + str(data[key]) + "'" | ||
|
||
key = ','.join(data.keys()) | ||
value = ','.join(data.values()) | ||
|
||
sql = "INSERT INTO " + table_name + " (" + key + ") VALUES (" + value + ")" | ||
|
||
return self.query(sql) | ||
|
||
# 提交事务,否则不能真正的插入数据 | ||
def commit(self): | ||
self.conn.commit() | ||
|
||
# 事务回滚 | ||
def rollback(self): | ||
self.conn.rollback() | ||
|
||
# 关闭数据库连接,释放资源 | ||
def close(self): | ||
self.cur.close() | ||
self.conn.close() | ||
|
||
|
||
''' | ||
if __name__ == "__main__": | ||
mysqlUtil = MySQLDB("localhost", "root", "uestc8020") | ||
mysqlUtil.select_db("test") | ||
#mysqlUtil.query("create table if not exists test1(name varchar(128) primary key, age int(4))") | ||
#mysqlUtil.insert("test1", {"name": "lisi", "age": 26}) | ||
#sql = "insert into test1(name, age) values ('%s', %d)" % ("张三", 21) | ||
#mysqlUtil.query(sql) | ||
print mysqlUtil.query_row("select * from test1") | ||
mysqlUtil.commit() | ||
mysqlUtil.close() | ||
if __name__ == "__main__": | ||
import config | ||
config.config_logger() | ||
mysqlUtil = MySQLDB("localhost", "root", "root") | ||
mysqlUtil.select_db("video_db") | ||
ret = mysqlUtil.query_row("select heat from bt where infohash='91f3657dfec65bd69bf27c5388ea9dadd23edf32'") | ||
print ret, type(ret) | ||
mysqlUtil.query("update bt set heat=10 wherenfohash='91f3657dfec65bd69bf27c5388ea9dadd23edf32'") | ||
mysqlUtil.commit() | ||
mysqlUtil.close() | ||
''' |
Oops, something went wrong.