Browse Source

the first commit

xiaozhu 4 years ago
parent
commit
543f616575
6 changed files with 117 additions and 0 deletions
  1. 49 0
      mod/mysql.py
  2. 37 0
      mod/news.py
  3. 7 0
      requirements.txt
  4. 24 0
      run.py
  5. BIN
      static/1.jpg
  6. BIN
      static/2.jpg

+ 49 - 0
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()

+ 37 - 0
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)

+ 7 - 0
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

+ 24 - 0
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()

BIN
static/1.jpg


BIN
static/2.jpg