diff --git a/mod/mysql.py b/mod/mysql.py new file mode 100644 index 0000000..78592af --- /dev/null +++ b/mod/mysql.py @@ -0,0 +1,49 @@ +# -*- coding: UTF-8 -*- +import pymysql + +class Mysql(object): + + def __init__(self): + + #配置mysql数据库信息 + config = { + 'host': '127.0.0.1', + 'port': 3306, + 'user': 'root', + 'passwd': '123456', + 'db': 'news', + 'charset': 'utf8' + } + + self.host = config['host'] + self.username = config['user'] + self.password = config['passwd'] + self.port = config['port'] + self.db = config['db'] + self.con = None + self.cursor = None + + try: + self.conn = pymysql.connect(**config) + # 所有的查询,都在连接 con 的一个模块 cursor 上面运行的 + self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor) + except: + print("DataBase connect error,please check the db config.") + + def exeCommit(self, sql=''): + """执行数据库sql语句,针对更新,删除,事务等操作失败时回滚 + """ + try: + execute = self.cursor.execute(sql) + self.conn.commit() + return execute + except pymysql.Error as e: + self.conn.rollback() + error = 'MySQL execute failed! ERROR (%s): %s' % (e.args[0], e.args[1]) + print("error:", error) + return error + + def __del__(self): + # 关闭cursor对象 + self.cursor.close() + self.conn.close() \ No newline at end of file diff --git a/mod/news.py b/mod/news.py new file mode 100644 index 0000000..91bccd9 --- /dev/null +++ b/mod/news.py @@ -0,0 +1,37 @@ +from mod.mysql import Mysql +import json +import datetime + +class News: + + #获取全部文章的方法 + + + def content(self): + mysql = Mysql() + sql = "SELECT * From news" + execute = mysql.exeCommit(sql) + if(execute >= 1): + cursor = mysql.cursor + result = cursor.fetchall() + del mysql + data = {'status': 'success', 'msg': '获取文章成功', 'data':result} + return json.dumps(data, cls=CJsonEncoder) + else: + del mysql + data = {'status': 'faild', 'msg': '没有文章' , 'data':None} + return json.dumps(data, cls=CJsonEncoder) + +class CJsonEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, datetime.datetime): + return obj.strftime('%Y-%m-%d %H:%M:%S') + elif isinstance(obj, datetime.date): + return obj.strftime("%Y-%m-%d") + else: + return json.JSONEncoder.default(self, obj) + +if __name__ == '__main__': + news = News() + result = news.content() + print(result) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7614147 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +cheroot==8.2.1 +jaraco.functools==3.0.0 +Markdown==3.1.1 +more-itertools==8.0.2 +PyMySQL==0.9.3 +six==1.13.0 +web.py==0.40 diff --git a/run.py b/run.py new file mode 100644 index 0000000..f36c9a7 --- /dev/null +++ b/run.py @@ -0,0 +1,24 @@ +# -*- coding: UTF-8 -*- +import web +from mod.news import News + +urls = ( + '/news','api', +) + +# render = web.template.render('templates') +app = web.application(urls, globals()) + + +#api的方法 +class api: + def GET(self): + news = News() + return news.content() + def POST(self): + news = News() + return news.content() + + +if __name__ == '__main__': + app.run() \ No newline at end of file diff --git a/static/1.jpg b/static/1.jpg new file mode 100644 index 0000000..c3bfdc2 Binary files /dev/null and b/static/1.jpg differ diff --git a/static/2.jpg b/static/2.jpg new file mode 100644 index 0000000..f60e6b1 Binary files /dev/null and b/static/2.jpg differ