-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (44 loc) · 1.35 KB
/
app.py
File metadata and controls
57 lines (44 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from flask import Flask
from flask.ext.httpauth import HTTPBasicAuth
from flask.ext.api import status
from json import dumps
from flask import request
from flask import Response
app = Flask(__name__)
auth = HTTPBasicAuth()
users = {
"admin": "root",
}
@auth.get_password
def get_pw(username):
if username in users:
return users.get(username)
return None
@app.route('/secret')
@auth.login_required
def secret_page():
return Response("Hello, %s!" % auth.username(), 201, mimetype='application/json')
@app.route('/')
def root_test():
return 'index'
@app.route('/user', methods=['GET'])
def get_users():
return dumps(users)
@app.route('/user/<string:username>', methods=['GET'])
def get_profile(username):
return username + " profile"
@app.route('/user/<string:username>', methods=['POST'])
def add_user(username):
users[username] = request.form['pwd']
resp = Response(dumps(username), status=200, mimetype='application/json')
return resp
@app.route('/user/<string:username>/pwd', methods=['PUT'])
def change_password(username):
resp = Response("Pasword changed" , status=200, mimetype='application/json')
if not users.has_key(username):
resp = Response("User " + username + " not found!" , status=403, mimetype='application/json')
else:
users[username] = request.form['pwd']
return resp
if __name__ == '__main__':
app.run()