-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
28 lines (21 loc) · 877 Bytes
/
server.py
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
from flask import Flask, request, make_response, jsonify
import hashlib
app = Flask(__name__)
# Dict of messages received by the server
received_messages = {}
@app.route('/messages', methods=['POST'])
def post_handler():
data = request.get_json()
sha256_digest = hashlib.sha256(data['message'].encode('utf-8')).hexdigest()
# Keep track of messages we've received.
received_messages[sha256_digest] = data['message']
return jsonify(digest=sha256_digest), 201
@app.route('/messages/<a_digest>', methods=['GET'])
def get_handler(a_digest):
if a_digest in received_messages:
return jsonify(message=received_messages[a_digest])
else:
return jsonify(err_msg="Message not found"), 404
if __name__ == '__main__':
context = ('localhost.crt', 'certs/key.pem')
app.run(debug=True, host='0.0.0.0', ssl_context=context)