How can I monitoring a flask app without modifying his code? - python

I've been making a little system to monitoring a flask app and others (postgres database, linux server, etc) with prometheus. Everything is going well, but I would like monitoring my flask app without modifying the code.
For example to monitoring methods of my app I did:
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
#app.route('/')
#REQUEST_TIME.time()
def index():
myUser = User.query.all()
return render_template('add_user.html', myUser= myUser)
I used this python library.
Also, I used other library to monitoring a flask app:
monitor(app, port=9999)
unfortunately both are modifying my code. I want to monitoring my flask app without modifying his code. It is possible?

It really isn't clear what you're asking. However, if you just need info about which requests are called and how long they take, you could run your flask app with newrelic: they offer a free tier (yay!) and using it you will gain lots of insight about your app. however to use it you'll need to run your app with their client (but no code changes are required).
more info on what you'll get can be found here:
https://newrelic.com/python/flask

Related

Auto reload flask server every half hour

I am creating a restful api using flask. I have a some data operations that need to be run before the server starts so that calling the api wont load the data again and again.
However, the data also updates via a cronjob. Since the updated data is the input the variable remains static as long as the flask app runs.
I am aware that the flask app reloads on code change but is there a way to make it reload periodically?
One possible, but maybe not the best solution could be:
run your Flask app via Supervisor ( http://supervisord.org/ )
after the cronjob finishes, kill your Flask app
Supervisor will automatically re-start your Flask app

How to run python scripts online?

I have a simple python script that gets the local weather forecast and sends an email with the data
I want to run this script daily, i found out that cron is used for this purpose but online cron jobs require a url
I wanted to ask how to host my python scripts so that they run online through a url, if possible that is...
I would recommend using Heroku with a python buildpack as a starting point. Using the flask library, you can very minimally start a web container and expose the endpoint online which can then be queried from your cron service. Heroku also provides a free account which ideally should fit your need.
As a peek into how easy it is to setup flask, well..
from flask import Flask
app = Flask(__name__)
#app.route('/cron-me')
def cron_me():
call_my_function_here()
return 'Success'
.. and you're done ¯\_(ツ)_/¯
Try setting up a simple Flask app at www.pythonanywhere.com, I think it will do the job for you even with the free account.
EDIT: And for sending e-mails, you can use Mailgun, with the free version you can send e-mails to a small number of addresses that need to be validated from the recipient side (to avoid people using it for spam :-))

multi request handling parallelly by using flask

#app.route("/")
def start():
#will do some task
return 'completed'
In the above program, after execution the 1st request 2nd request will execute. But I want to make such a server that will accept, execute and response multiple requests at a certain time parallelly by using flask or anything else.
How will I make this?
For multi-request handling/production deployment, gunicorn or apache or gevent has to be used.
http://flask.pocoo.org/docs/0.11/deploying/
Similar approach follows for other python web frameworks too like Django.
You can use klein module, which handle multiple requests in a time.
Please refer the following lin which will give clear explanation
about limiting in FLASK.
Comparison between Flask and Klein
After refering this link I switched from Flask to Klein. Hope it helps you too.

Flask app serve as two different apps

I had worked with the Flask application which functions as an admin panel and an API. My admin panel includes login page and bunch of admin stuff in it. So I don't want to expose it to the internet.Admin panel should be only accessible from the intranet however my API should be accessible from the internet.
I have two machines.One is a local machine and other one will be hosted at the AWS. The problem is that the code will be same in the two machines however one will serve as an API and other will serve as an Admin panel.
My supervisor told me that I can use "Flask blueprints" to achieve what I am trying to do but I want to be sure before starting to implement.
Can Flask blueprints solve this problem or are there any other options?
(One thing comes to mind is to separate the API from the admin panel into two different Flask apps. Which is easy to do and solves everything. However I am unable to do that right now. )
Image of what I am trying to do
You can use before_app_request on your blueprint and check if your client ip starts with 192.168 or 10.0 or 172.16
from flask import abort
#blueprint_1.before_request
def check_network():
if not request.remote_addr.startswith("192.168") or ...:
abort(404)
Edit : According to Blueprint documentation before_app_request Such a function is executed before each request, even if outside of a blueprint.
use before_request instead (it will be executed before request on blueprint_1 Blueprint ... i edited my code ...

Flask: A RESTful API and SocketIO Server

Background
I am trying to create a simple REST API using the Flask-RESTful extension. This API will be working primarily to manage the CRUD and authentication of users for a simple service.
I am also trying to create a few web sockets using the Flask-SocketIO extension that these users will be able to connect to and see real-time updates for some data related to other people using the service. As such, I need to know that these users are authenticated and authorized to connect to certain sockets.
Problem
However, I'm having a bit of trouble getting set up. It seems like I am not able to have these two components (the REST API and SocketIO server) work together on the same Flask instance. The reason I say this is because when I run the following, either the REST API or the SocketIO server will work, but not both:
from flask import Flask
from flask_restful import Api
from flask.ext.socketio import SocketIO
app = Flask(__name__)
api = Api(app)
socketio = SocketIO(app)
# some test resources for the API and
# a test emitter statement for the SocketIO server
# are added here
if __name__ == '__main__':
app.run(port=5000)
socketio.run(app, port=5005)
Question
Is the typical solution for this type of setup to have two distinct instances of Flask going at the same time? For instance, would my SocketIO server have to make requests to my REST API in order to check to see that a specific user is authenticated/authorized to connect to a specific socket?
You just want to run socketio.run(app, port=5005) and hit the REST API on port 5005.
The reason this works is because under the hood, Flask-SocketIO is running an evented webserver based on gevent (or with the 1.0 release, also eventlet) - this server handling the websocket requests directly (using the handlers you register via the socketio.on decorator) and is passing on the non-websocket requests to Flask.
The reason your code wasn't working is because both app.run and socketio.run are blocking operations. Whichever one ran first was looping, waiting for connections, never allowing the second to kick off. If you really needed to run your websocket connections on a different port you'd need to spawn either the socketio or the app run call on a different process.

Categories

Resources