Skip to content

Instantly share code, notes, and snippets.

@fduxiao
Created August 7, 2017 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fduxiao/984e58c16bf1850e816f4a2c28ebcaab to your computer and use it in GitHub Desktop.
Save fduxiao/984e58c16bf1850e816f4a2c28ebcaab to your computer and use it in GitHub Desktop.
mongo data receiver
#!/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