Created
May 21, 2015 03:50
-
-
Save pandada8/95efad6713a4e768efa0 to your computer and use it in GitHub Desktop.
Simple Python Server.py
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
from flask import request, Flask, jsonify | |
import json | |
from peewee import SqliteDatabase, MySQLDatabase, Model, TextField | |
app = Flask(__name__) | |
db = SqliteDatabase("test.db") | |
# db = MySQLDatabase("DATABASENAME", host="localhost", user="user", passwd='password', charset='utf8mb4') | |
class Data(Model): | |
json_text = TextField() | |
class Meta(): | |
database = db | |
db.create_tables([Data], safe=True) | |
@app.before_request() | |
def connect_db(): | |
db.connect() | |
@app.after_request(): | |
def close_db(): | |
if not db.is_closed(): | |
db.close() | |
@app.route("/post_target", methods=["POST"]) | |
def post_result(): | |
print(request.body) | |
data = json.loads(request.body) | |
# save the data | |
Data.create(json_text=json.dumps(data)) | |
return jsonify(err=0, msg="成功") | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment