Deploying Flask app to Heroku - python

I'm trying to develop my first "large" app with Flask on Heroku and I'm attempting to combine the basic tutorial here: https://devcenter.heroku.com/articles/python with the instructions here: http://flask.pocoo.org/docs/patterns/packages/#larger-applications. It works locally with "foreman start" but when I push to Heroku I get an error that the wrong port is in use:
Starting process with command python run.py
2012-12-04T23:45:18+00:00 app[web.1]: * Running on
http://127.0.0.1:5000/ 2012-12-04T23:45:18+00:00 app[web.1]: *
Restarting with reloader 2012-12-04T23:45:23+00:00 heroku[web.1]:
Error R11 (Bad bind) -> Process bound to port 5000, should be 33507
(see environment variable PORT)
I'm new to all this, but it looks like it's trying to run "locally" on Heroku. I've tried all sorts of combinations, but can't get it to work. My very simple code right now is (the app is called "pml"):
directory: /pml
Procfile:
web: python run.py
run.py:
from pml import app
app.run(debug=True)
directory: /pml/pml/
__init__.py
from flask import Flask
app = Flask(__name__)
import pml.views
view.py
from pml import app
#app.route('/')
def index():
return 'Hello World!'

I haven't used Heroku, but to me, it looks like they have a reserved port for Flask, specifically 33507. It looks like it will try to use an environment variable, which I am not sure how to set in Heroku. The good news is you can tell Flask which port to use.
try this:
app.run(debug=True, port=33507)
and it looks like adding the PORT to the env in heroku is done like this:
heroku config:add PORT=33507
You should only have to do one of these. I would try the first as it, to me, is the straight forward way to fix the issue.
EDIT
After reading the article from your post, I see where the issue comes in.
port = int(os.environ.get('PORT', 5000))
That line says, get the value of PORT from the environment if it is set, otherwise use 5000. I am not sure why they wouldn't allow it to run from 5000 if that's what is in their docs, but I would try this change:
port = int(os.environ.get('PORT', 33507))

Related

How to deploy python application to evennode?

After reading following docs and examples about deploying python app to evennode I've tried to do it with Flask application, but didn't succeed
https://www.evennode.com/docs/git-deployment
https://github.com/evennode/python-getting-started
Here is my main.py module's code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)
Also I'have created conf.py file with configuration for gunicorn:
workers = 4
bind = "0.0.0.0:8000"
Then I'm running the application with gunicorn --config=conf.py main:app and all works well on my local machine. To run it on evennode I populated requirements.txt and committed above files. Then run following commands:
git remote add evennode git#git.evennode.com:your_app_here
git push evennode master
The output looks next way and I don't know what to do with it:
ssh: connect to host git.evennode.com port 8000: No route to host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have added my public ssh key to evennode app settings as well, so that's can't be an issue
Any help is appreciated

Sample Bottle App (Python) gives 404 even though route is defined

I am just getting started with Bottle. I have found a sample app on GitHub. The sample just has a server.py file which looks like below
import bottle
APP = bottle.Bottle()
#APP.get('/')
def index():
return '<p>Hello</p>'
if __name__ == '__main__':
bottle.run(application=APP)
The requirements.txt file has
bottle
gunicorn
as dependencies. I am using Python 3.7.2. After running pip install -r requirements.txt, I ran python server.py. The server starts up without error on Port 8080
Bottle v0.12.18 server starting up (using WSGIRefServer(application=<bottle.Bottle object at 0x1040fa2d0>))...
Listening on http://127.0.0.1:8080/
When I access http://localhost:8080, I get
Error: 404 Not Found
Sorry, the requested URL 'http://localhost:8080/' caused an error:
Not found: '/'
Please let me know what's wrong with the setup.
Just replace bottle.run(application=APP) application arg name to app
bottle.run(app=APP)

Serving Flask app with waitress on windows

I am able to run a webserver using the following code
from flask import Flask
from waitress import serve
app = Flask(__name__, static_url_path='/static')
...
serve(app, port=8080)
The problem is that I can access it only from the machine where it is running, if I try to access it using the ipv4 ip, it doesn't work. Am I missing a step?
Simple example,try it!
I hope it will help you.
app1.py
from flask import Flask
app = Flask(__name__)
# app.run(host='0.0.0.0', port=8080,debug=True)
waitress_server.py
from waitress import serve
import app1
serve(app1.app, host='0.0.0.0', port=8080)
Then run below command
python waitress_server.py
Waitress now provides a simple command line Utility called waitress-serve for running the Flask Application. Please note that this answer is valid for Waitress 1.30. The command line arguments could change in future.
If your Flask application is called myapplication and the method which instantiates your application is called create_app, then you can run the command below. This will launch the server listening on port 8080 by default.
waitress-serve --call "myapplication:create_app"
If you wish to launch it on port 80 (http), then all you need to do is:
waitress-serve --port=80 --call "myapplication:create_app"
D:\flaskapps>waitress-serve --port 80 --call "dlrlsummarizer:create_app"
Serving on http://ADITHYA-PC:80
Waitress serve command line arguments.
Flask 1.0 production deployment tutorial.
Try using
serve(app, host='0.0.0.0', port=8080)
I ran into this question and looking something similar.
After looking at the documentation and combined with your original request, I tested
serve(app, port=8080, host="x.x.x.x")
Where x.x.x.x is my host ip address.
It works fine on my end.
Complete code
from flask import Flask
from waitress import serve
app = Flask(__name__)
...
serve(app, port=8080, host="x.x.x.x")
I realize this question was probably based in a miss-diagnosed firewall or NAT issue, but in case people come here actually wanting to serve a Flask app with waitress on windows properly (as a service), I want to point to my answer here, so that it can be of use and receive some feedback.
from flask import Flask
from waitress import serve
app = Flask(__name__)
#app.route("/")
def hello_world():
return "<p>Hello stay healthy.</p>"
if __name__ == "__main__":
serve(app, host="127.0.0.1", port=8080)
Problem may persist in host. You can use host="127.0.0.1" in your program.
Save your program in app.py file.
Run your program.
The server will be accessible at http://localhost:8080
I just realized that can be reached by computers in the same network, but not from computers outside of the network
You need to forward the port in your router and use your public IP address.
To be able to use your internal PC (behind the router) you need to forward in the router the externl port 8080 to internal port 8080 and IP address of your server.
In this conditions you can access your server from outside your network using your external IP. That is OK if you have a static IP address allocated by your provider. If not than you can use a free DNS provider (I use DnsExit) which will provide you with a name for your external IP address. This way you can access your server with a name even if the IP address from your service provider changes from time to time.

running python+flask on heroku, without gunicorn [duplicate]

I'm trying to develop my first "large" app with Flask on Heroku and I'm attempting to combine the basic tutorial here: https://devcenter.heroku.com/articles/python with the instructions here: http://flask.pocoo.org/docs/patterns/packages/#larger-applications. It works locally with "foreman start" but when I push to Heroku I get an error that the wrong port is in use:
Starting process with command python run.py
2012-12-04T23:45:18+00:00 app[web.1]: * Running on
http://127.0.0.1:5000/ 2012-12-04T23:45:18+00:00 app[web.1]: *
Restarting with reloader 2012-12-04T23:45:23+00:00 heroku[web.1]:
Error R11 (Bad bind) -> Process bound to port 5000, should be 33507
(see environment variable PORT)
I'm new to all this, but it looks like it's trying to run "locally" on Heroku. I've tried all sorts of combinations, but can't get it to work. My very simple code right now is (the app is called "pml"):
directory: /pml
Procfile:
web: python run.py
run.py:
from pml import app
app.run(debug=True)
directory: /pml/pml/
__init__.py
from flask import Flask
app = Flask(__name__)
import pml.views
view.py
from pml import app
#app.route('/')
def index():
return 'Hello World!'
I haven't used Heroku, but to me, it looks like they have a reserved port for Flask, specifically 33507. It looks like it will try to use an environment variable, which I am not sure how to set in Heroku. The good news is you can tell Flask which port to use.
try this:
app.run(debug=True, port=33507)
and it looks like adding the PORT to the env in heroku is done like this:
heroku config:add PORT=33507
You should only have to do one of these. I would try the first as it, to me, is the straight forward way to fix the issue.
EDIT
After reading the article from your post, I see where the issue comes in.
port = int(os.environ.get('PORT', 5000))
That line says, get the value of PORT from the environment if it is set, otherwise use 5000. I am not sure why they wouldn't allow it to run from 5000 if that's what is in their docs, but I would try this change:
port = int(os.environ.get('PORT', 33507))

Pip installed dependencies not available to application file

I'm having problems getting dependencies installed on Heroku. I have a Python app that I want to deploy but every time I try and load the site I get ImportError's for both flask and sqlalchemy. I get these same errors if I run: heroku run python and try and import the modules interactively. I'm very puzzled and can't seem to solve this issue I was hoping someone else would know a solution. The details of the relevant files are down below.
Profile:
web: python app.py
requirements.txt:
Flask==0.9
Jinja2==2.6
SQLAlchemy==0.7.9
Werkzeug==0.8.3
app.py:
import os, flask, sqlalchemy
from gateway import server
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
server.app.run(host='0.0.0.0', port=port)
EDIT: I ended up restarting my application by following the Heroku instructions step-by-step and then copying over all my old code. This seemed to fix the problem for me so my problems seem to be a weird edge case.
I think your error may be in the "from gateway import server". It's not clear to me what that line's doing, and that code doesn't work when I try to run it locally on my machine.
Here's similar code, but in a format that's more familiar to me - I've deployed this to Heroku successfully in the past:
import os
from flask import Flask
from flask import render_template
app = Flask (__name__)
#app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Can you run this code locally?
Check the Heroku Flash tutorial
https://devcenter.heroku.com/articles/python

Categories

Resources