Created
August 7, 2017 01:49
-
-
Save fduxiao/984e58c16bf1850e816f4a2c28ebcaab to your computer and use it in GitHub Desktop.
mongo data receiver
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
#!/usr/bin/env python3 | |
from flask import * | |
import pymongo | |
app = Flask(__name__) | |
def get_client(): | |
if not hasattr(g, 'mongo_client'): | |
g.mongo_client = pymongo.MongoClient() | |
return g.mongo_client | |
def get_coll(coll='coll', dbname='uqer'): | |
client = get_client() | |
db = client[dbname] | |
return db[coll] | |
@app.route('/<collection>', methods=['GET', 'POST']) | |
def push_request(collection): | |
if request.method == 'GET': | |
return collection | |
elif request.method == 'POST': | |
data = request.get_json() | |
coll = get_coll(collection) | |
if type(data) is list: | |
bulk = coll.initialize_unordered_bulk_op() | |
for line in data: | |
bulk.insert(line) | |
bulk.execute() | |
elif type(data) is dict: | |
coll.insert_one(data) | |
return jsonify(code=0) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment