Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from flask import Flask
- from flask import jsonify
- from flask import request
- import pymongo
- import json
- from bson import json_util
- app = Flask(__name__)
- @app.before_request
- def abc():
- auth = request.authorization
- if auth.username == 'admin' and auth.password == 'abc123':
- # response = jsonify({"message":"success"})
- # response.status_code = 200
- # return response
- pass
- else:
- response = jsonify({"message":"Invalid credentials"})
- response.status_code = 401
- return response
- def connecttomongodb():
- conn=pymongo.MongoClient('localhost', 27017)
- db = conn.student_database
- coll = db.students
- return coll
- @app.route('/students/<name>', methods = ['GET'])
- def test(name):
- temp = connecttomongodb()
- var =temp.find_one({"name":name})
- return json_util.dumps(var)
- @app.route('/students/',methods = ['POST'])
- def test123():
- var = request.json
- temp = connecttomongodb()
- temp.insert(var)
- print(var)
- return 'good morning ' + str(request.json)
- @app.route('/students/',methods = ['GET'])
- def test123():
- temp = connecttomongodb()
- var = temp.find()
- return json_util.dumps(var)
- @app.route('/students/<name>', methods = ['DELETE'])
- def test_delete(name):
- return 'delete success ' + name
- @app.route('/students/<name>', methods = ['PUT'])
- def test_put(name):
- var = request.json
- temp = connecttomongodb()
- temp.update({"name":"palo"},{'$set':var})
- # db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
- return 'put success ' + name + str(var)
- if __name__ == '__main__':
- app.run('localhost',8080,True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement