-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
33 lines (26 loc) · 718 Bytes
/
server.py
File metadata and controls
33 lines (26 loc) · 718 Bytes
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
# The simplest Python API server
from flask import Flask, jsonify, request
app = Flask(__name__)
# Simple GET endpoint
@app.route('/')
def home():
return jsonify({"message": "Hello, World!"})
# GET with path parameter
@app.route('/user/<name>')
def get_user(name):
return jsonify({"user": name})
# POST endpoint
@app.route('/data', methods=['POST'])
def post_data():
data = request.get_json()
return jsonify({"received": data}), 201
# GET with query parameters
@app.route('/search')
def search():
query = request.args.get('q', 'default')
return jsonify({"query": query})
if __name__ == '__main__':
app.run(debug=True, port=5000)
# To run it:
# pip3 install flask
# python3 server.py