|
| 1 | +fromflaskimportFlask,render_template |
| 2 | +fromdatetimeimportdatetime |
| 3 | +importrandom |
| 4 | + |
| 5 | +app=Flask(__name__) |
| 6 | + |
| 7 | +# Simple counter to track API requests |
| 8 | +request_counter=0 |
| 9 | + |
| 10 | +@app.route('/') |
| 11 | +defhome(): |
| 12 | +returnrender_template('index.html') |
| 13 | + |
| 14 | +@app.route('/api/data') |
| 15 | +defapi_data(): |
| 16 | +globalrequest_counter |
| 17 | +request_counter+=1 |
| 18 | + |
| 19 | +# Sample data that simulates real-world API responses |
| 20 | +server_statuses= ["Healthy","Busy","Maintenance","Optimal"] |
| 21 | +sample_cities= ["New York","London","Tokyo","Sydney","Paris","Toronto"] |
| 22 | + |
| 23 | +return { |
| 24 | +'message':f'Hello from Flask! Request #{request_counter}', |
| 25 | +'framework':'Flask', |
| 26 | +'language':'Python', |
| 27 | +'timestamp':datetime.now().strftime('%Y-%m-%d %H:%M:%S'), |
| 28 | +'server_status':random.choice(server_statuses), |
| 29 | +'temperature':random.randint(15,35),# Celsius |
| 30 | +'city':random.choice(sample_cities), |
| 31 | +'users_online':random.randint(50,500), |
| 32 | +'total_requests':request_counter |
| 33 | + } |
| 34 | + |
| 35 | +if__name__=='__main__': |
| 36 | +app.run(debug=True,host='localhost',port=5000) |