-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlask_Python.pl
220 lines (185 loc) · 5.1 KB
/
Flask_Python.pl
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from flask import Flask, url_for
from flask import request
from flask import json
from flask import jsonify
from flask import Response
import logging
# Start of Authentication
from functools import wraps
# End of Authentication
app = Flask(__name__)
#Start for Logging
file_handler = logging.FileHandler('app.log')
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
app.run(debug=True)
# End of Logging
'''
http://blog.luisrei.com/articles/flaskrest.html
curl http://127.0.0.1:5000/
curl -X PATCH http://127.0.0.1:5000/echo
curl -H "Content-type: application/json" -X POST http://127.0.0.1:5000/messages -d '{"message":"Hello Data"}'
curl -H "Content-type: application/octet-stream" -X POST http://127.0.0.1:5000/messages --data-binary @message.bin
curl -i http://127.0.0.1:5000/hello
curl -v -u "admin:secret" http://127.0.0.1:5000/secrets
'''
# First method for Pages
'''
GET /
Welcome
GET /articles
List of /articles
GET /articles/123
You are reading 123
'''
@app.route('/')
def api_root():
return 'Welcome'
@app.route('/articles')
def api_articles():
return 'List of ' + url_for('api_articles')
@app.route('/articles/<articleid>')
def api_article(articleid):
return 'You are reading ' + articleid
# Second Method for GET Requests
'''
GET /hello
Hello John Doe
GET /hello?name=Luis
Hello Luis
'''
@app.route('/hello')
def api_hello():
if 'name' in request.args:
return 'Hello ' + request.args['name']
else:
return 'Hello John Doe'
# Third Method for Request Methods (HTTP Verbs)
'''
curl -X PATCH http://127.0.0.1:5000/echo
GET /echo
ECHO: GET
POST /ECHO
ECHO: POST
'''
@app.route('/echo', methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'])
def api_echo():
if request.method == 'GET':
return "ECHO: GET\n"
elif request.method == 'POST':
return "ECHO: POST\n"
elif request.method == 'PATCH':
return "ECHO: PATCH\n"
elif request.method == 'PUT':
return "ECHO: PUT\n"
elif request.method == 'DELETE':
return "ECHO: DELETE"
# Fourth Method for Request Data & Headers
'''
curl -H "Content-type: application/json" \
-X POST http://127.0.0.1:5000/messages -d '{"message":"Hello Data"}'
curl -H "Content-type: application/octet-stream" \
-X POST http://127.0.0.1:5000/messages --data-binary @message.bin
'''
@app.route('/messages', methods = ['POST'])
def api_message():
if request.headers['Content-Type'] == 'text/plain':
return "Text Message: " + request.data
elif request.headers['Content-Type'] == 'application/json':
return "JSON Message: " + json.dumps(request.json)
elif request.headers['Content-Type'] == 'application/octet-stream':
f = open('./binary', 'wb')
f.write(request.data)
f.close()
return "Binary message written!"
else:
return "415 Unsupported Media Type ;)"
# Fifth Method for Response
'''
curl -i http://127.0.0.1:5000/hello
'''
@app.route('/helloResponse', methods = ['POST'])
def helloResponse():
data = {
'hello' : 'world',
'number' : 3
}
js = json.dumps(data)
# resp = Response(js, status=200, mimetype='application/json')
resp = jsonify(data)
resp.status_code = 200
resp.headers['Link'] = 'http://luisrei.com'
return resp
# Sixth Method for Status And Error Codes
'''
GET /users/2
HTTP/1.0 200 OK
{
"2": "steve"
}
GET /users/4
HTTP/1.0 404 NOT FOUND
{
"status": 404,
"message": "Not Found: http://127.0.0.1:5000/users/4"
}
'''
@app.errorhandler(404)
def not_found(error=None):
message = {
'status': 404,
'message': 'Not Found: ' + request.url,
}
resp = jsonify(message)
resp.status_code = 404
return resp
@app.route('/users/<userid>', methods = ['GET'])
def api_users(userid):
users = {'1':'john', '2':'steve', '3':'bill'}
if userid in users:
return jsonify({userid:users[userid]})
else:
return not_found()
# Start for Logging
@app.route('/hello', methods = ['GET'])
def api_loghello():
app.logger.info('informing')
app.logger.warning('warning')
app.logger.error('Error!')
return "check your logs\n"
# End for Logging
# Start of Authentication
def check_auth(username, password):
return username == 'admin' and password == 'secret'
def authenticate():
message = {'message': "Authenticate."}
resp = jsonify(message)
resp.status_code = 401
resp.headers['WWW-Authenticate'] = 'Basic realm="Example"'
return resp
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth:
return authenticate()
elif not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
@app.route('/secrets')
@requires_auth
def api_helloAuthentication():
return "Shhh this is top secret spy stuff!"
# End of Authentication
'''
GET /secrets
HTTP/1.0 401 UNAUTHORIZED
WWW-Authenticate: Basic realm="Example"
{
"message": "Authenticate."
}
curl -v -u "admin:secret" http://127.0.0.1:5000/secrets
'''
if __name__ == '__main__':
app.run()