Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, jsonify, abort
- app = Flask(__name__)
- app.secret_key = "very_secret"
- import random
- def fake_sensor(sid):
- '''
- Read sensor with sensorid
- and return the temperature
- '''
- return random.randint(0,21)
- @app.route('/')
- def main():
- html = """<html>
- <head>
- <meta charset="utf-8">
- <title>Very importand REST Service</title>
- </head>
- <h1>Sensor REST-Service</h1>
- <ul>
- <li><a href="sensor/1">Sensor 1</a></li>
- <li><a href="sensor/2">Sensor 2</a></li>
- <li><a href="sensor/3">Sensor 3</a></li>
- <li><a href="sensor/4">Sensor 4</a></li>
- </ul>
- </html>"""
- return html
- @app.route('/sensor/<int:sid>')
- def read_sensor(sid):
- '''
- Return the temperature as json
- '''
- if not 1 <= sid <= 4:
- abort(404)
- temp = fake_sensor(sid)
- return jsonify({'sid': sid, 'temp': temp})
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement