Serve a Flask app on a sub-url during localhost development - python

I have a Flask app as backend that serves a REST API and an AngularJS front-end app.
I use Grunt/Livereload to serve the front-end at the address: http://localhost:5000/
Is it possible to serve the Flask app on a sub-url of localhost during development, using app.run() or run_simple from werkzeug?
Specifically I would like to have the Flask app accessible at the address: http://localhost:5000/api
I found this solution but it has the disadvantage of serving a dummy app at http://localhost:5000/ which uses the address and doesn't let me serve the AngularJS app at that address.

The way that Flask and Yoman are set up you cannot actually do this - it is possible to have two processes share the same port, but it is generally done to allow one master process to pass off handling of individual requests to sub-processes, which is not quite what you are doing here. (In general, in production, you would run both the front and back end behind a proxy server like nginx.)
Fortunately, you do not have to re-invent the wheel or run a separate proxy server just to develop your app - there is a Grunt plugin called grunt-connect-proxy that will let you proxy requests to a sub-url to another location entirely. That will let you spin up your Flask backend server on a different port (say port 5001) and proxy requests to localhost:5000/api (for example) to localhost:5001/:
connect: {
options: {
port: 5000,
hostname: 'localhost'
},
proxies: [
{
context: '/api',
host: '127.0.0.1',
port: 5001,
https: false,
changeOrigin: false,
xforward: false
}
]
}
And then you can run your Flask app with app.run(port=5001).

This worked for me
from flask import Flask
prefix = '/abc'
app = Flask(__name__)
# redefine route
def route(path, *args, **kwargs):
return _route(prefix + path, *args, **kwargs)
_route = app.route
app.route = route
# Test function
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()

Related

Can't access Flask app running on 0.0.0.0 on GCE

I set Firewall Rule for my local google compute instance at host '0.0.0.0' and port 7000.
And I executed python server.py, it was running on https://0.0.0.0:7000
but when I enter https://external-ip:7000 on my local browser it did not work.
So how can I run flask on google compute engine and open in my local computer browser?
server.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World’
if __name__ == '__main__':
app.run(debug=1,port=7000,host='0.0.0.0')
A few things:
Check your VPC firewall:
https://cloud.google.com/vpc/docs/firewalls
In your terminal, see if connections are working locally on that host by issuing:
telnet localhost 7000
If it connects then it's either firewall or the below.
If you're running on https, you'll probably need to have something along the lines of:
context = ('host.crt', 'host.key')
app.run(host='0.0.0.0',port='7000', ssl_context=context)
Lastly, it's https:// not \

Running a Flask app on nginx

I don't know exactly what I am doing but I am experimenting with running Flask on nginx. I am boiling it down to the simple bit of code below. First I have a test app in Flask like this:
from flask import Flask, render_template
app = Flask(__name__, static_folder='client', template_folder='client/html')
def show_home_page():
return render_template("home.html")
#app.route('/')
def server():
return show_home_page()
if __name__ == '__main__':
app.run(threaded=True)
If I run python app.py I can go to http://localhost:5000 and see the "Hello World". Next I read that I need to run uwsgi but its not clear what params I need to pass to it. I tried different things like:
uwsgi -s /tmp/app.sock --manage-script-name --mount ./=app:app
I also noted that I need to set my nginx conf file to match, but I am stuck on that as well (I just get a welcome from nginx on port 5000) and it doesnt seem to link with my Flask app. I googled around a bit but nothing has clicked yet.
server {
listen 5000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
You can use refer this for Flask with Nginx and uWSGI:
Python flask with Nginx and uWSGI

How to have one Flask app listen on two different ports?

Is it possible to have a single flask app with routes on two different ports? My Flask app needs to listen for webhooks and due to some security biz it can't receive foreign POST requests on the default port. Is it possible to do something like this?
#app.route('/hook/<sourcename>', methods=["POST"], port=5051)
def handle_hook(sourcename):
print 'asdf'
If you don't need any socket code inside C plugins, gevent could help, e.g. with
import gevent
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
https_server = WSGIServer((HOST, HTTPS_PORT), app, keyfile=PRIVKEY, certfile=CERT)
https_server.start()
http_server = WSGIServer((HOST, HTTP_PORT), app)
http_server.start()
while True:
gevent.sleep(60)
A server by default only listens to a single port. Wouldn't it make more sense, since the additional port requires additional functionality, to implement a front-end server on the second port that proxies the POST request locally? There are many well-documented ways to do this such as this one

Flask with gevent multicore

What is the clear way to run flask application with gevent backend server and utilize all processor cores? I have idea to run multiple copies of flask application where gevent WSGIServer listen one port in diapason 5000..5003 (for 4 processes) and nginx as load balancer.
But I'm not sure that this way is the best and may be there are some other ways to do it. For example, master process listen one port and workers process incoming connections.
I'll take a shot!
Nginx!
server section:
location / {
include proxy_params;
proxy_pass http://127.0.0.1:5000;
}
Flask App
This is a simple flask app that i will be using for this example.
myapp.py:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
uWSGI
Okay so I know that you said that you wanted to use gevent, but if you are willing to compromise on that a little bit I think you would be very happy with this setup.
[uwsgi]
master = true
plugin = python
http-socket = 127.0.0.1:5000
workers = 4
wsgi-file = myapp.py
callable = app
Gunicorn
If you must have gevent you might like this little setup
config.py:
import multiprocessing
workers = multiprocessing.cpu_count()
bind = "127.0.0.1:5000"
worker_class = 'gevent'
worker_connections = 30
Then you can run:
gunicorn -c config.py myapp:app
Thats right you have a worker for each cpu and 30 connections per worker.
See if that works for you.
If you are really sold on using nginx as a load balancer try something like this in your http section
upstream backend {
server 127.0.0.1:5000;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
server 127.0.0.1:5004;
}
then one of these in the server section
location / {
include proxy_params;
proxy_pass http://backend;
}
Good Luck buddy!

nginx + uwsgi + flask - disabling custom error pages

Is it possible to disable nginx's custom error pages - if I may call them that - to display my framework's exception pages?
I can't really see my werkzeug debugger tool rendered in html...
UPDATE
OK, I got to make a very very simple flask application to work and I'll post the bits:
/home/my_user/.virtualenvs/nginx-test/etc/nginx.conf
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 5000;
server_name localhost;
access_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/access.log;
error_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/error.log;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
}
/home/my_user/dev/nginx_test/___init___.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
raise Exception()
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)
PYTHONPATH environment variable:
$ echo $PYTHONPATH
/home/my_user/dev/
How I run uwsgi:
$ uwsgi -s /tmp/uwsgi.sock --module nginx_test --callable app
How I run nginx:
$ nginx -c ~/.virtualenvs/nginx-test/etc/nginx.conf -p ~/.virtualenvs/nginx-test/lib/nginx/
If I hit the root page:
If I run nginx manually like:
python /home/my_user/dev/nginx_test/___init___.py
I will see instead, and what I want to see:
Of course I made sure it would work when I didn't raise the exception, but returned 'Hello World' for example on my index() function.
This is referred to custom error pages in .NET. I want to disable this and let nginx/uwsgi pass the html generated by the debugger directly to the browser instead of the internal server error thing.
UPDATE 2
Now if I change my flask app to enable debugging mode by:
/home/my_user/dev/nginx_test/___init___.py
from flask import Flask
app = Flask(__name__)
app.config.update(DEBUG=True)
#app.route('/')
def index():
raise Exception()
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)
Then I get 502 error.
But if I instead of raise Exception:
/home/my_user/dev/nginx_test/___init___.py
from flask import Flask
app = Flask(__name__)
app.config.update(DEBUG=True)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)
I get 'Hello World' on my browser when I hit the page (http://localhost:5000).
This "Internal Server Error" page is not from nginx but from Flask. It does so when debug mode is off.
uwsgi is importing your code as a module, not running at as a script. __name__ == '__main__' is False and the if statement is not executed. Try setting debug mode outside of the if:
app = Flask(__name__)
app.debug = True
However, it is strongly recommended to never leave the debug mode on a server on the public internet, since the user can make the server run any code. This is a serious security issue.
Use Flask#errorhandler to register your own error handlers in flask. For example to replace the 404 you would do something like:
app = Flask()
#app.errorhandler(404)
def handel_404(error):
return render_template('404.html')
Simon Sapin has really given you the correct answer. You need to enable debug in Flask. Nginx does not return any custom error pages unless you explictly configure it to do so.
If you use the following code you will see your debug messages from flask, proxied via Nginx.
from flask import Flask
app = Flask(__name__)
app.debug = True
#app.route('/')
def index():
raise Exception()
As per your update 2. You are seeing a 502 (bad gateway) because Flask is simply not returning any response at all, or a response that Nginx does not understand. A 502 is not a problem with Nginx. It means that whatever Nginx is trying to talk (your flask app in this case) is not working properly at all.
However, in many ways you shouldn't be doing this. Debugging mode should only be enabled when you are running flask on your local dev machine. Which is the whole point of the if __name__ == "__main__": line anyway.

Categories

Resources