flask - Unable to connect mogodb in heroku - python

I have an application using Flask and MongoEngine on heroku.
My configuration looks like this:
from flask import Flask
from flask.ext.mongoengine import MongoEngine
app = Flask(__name__)
#am setting proper configuration based on database uri
if os.environ.get('MONGOLAB_URI'):
app.config.from_object(herokuConfig)
else:
app.config.from_object(localConfig)
MongoEngine(app)
This was working fine up to yesterday, but this morning I am unable to run the application.
After looking into heroku debug logs, I observed AutoReconnect: not master error, but I am able to run it locally.
I have read some documentation regarding the above error occurring due to a read_preference setting.
I am unable to solve this, and am looking forward to suggestions.

Related

Deploying flask app on cpanel, INTERNAL SERVER ERROR when changing requesting other than base url

I want to deploy my flask-restx application on a shared hosting. Since I am beginner in deployment, I followed a video tutorial from youtube.
I did step by step by following this tutorial.
For those who do not want to go through the tutorial, I am writing the steps:
I created an application from the Python cPanel
Initial set up in Cpanel
Then I opened terminal and changed my venv and installed flask by "pip install flask"
Project Structure
filas_folder/
├──public
├──tmp
│ └──restart.txt
├──app.py
└──passenger_wsgi.py
app.py looks like
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main_():
return "flask is running"
#app.route("/user")
def main_2():
return "user is running"
if __name__ == "__main__": app.run()
Restart app from cpanel
passenger.py looks like
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'app.py')
application = wsgi.app
when I open www.example.com
flask is running
But when I open www.example.com/user
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#example.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My system has cloudlinux and uses apache server. This is not the first deployment. Many wordpress and static websites are running on the server.
I opened apache logs at /usr/local/apache/logs/error_log
I get the error "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer" http://example.com/user"
Add the following to the top of your .htaccess file:
RewriteEngine on
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]
Got this info from: https://stackoverflow.com/a/63971427/10122266

"Connection in use" error in GCP app engine

I am new to GCP App engine. I am trying to make web server using flask on App engine. I tested the version of my code on localhost and it is working fine. But when I am trying to deploy it in the App Engine of GCP it is giving me this strange error "app logs".
error logs
Here is my code for the flask
app.run(threaded = True, host='127.0.0.1', port=80)
Thanks!!
You don't need to call app.run() in your main.py file -- App Engine will do that for you. Simply initialize the app variable in this file instead.

Solution needed to the 403 forbidden error I'm getting after deploying flask app on AWS elasticbeanstalk

I need solution to the 403 error I'm getting after successfully deploying flask app on AWS elasticbeanstalk. After deploying a flask application developed in visual studio it gives 403 forbidden error. I've tried different searches to solve the problem but they haven't resolved it.
For example, it was suggested somewhere the start-off python file should be named - application.py, which I did and redeployed but it still didn't work. It was also mentioned in another example that the deployed solution was pointing to a folder instead of the start-off file, which is not the case in my situation.
Possible problems pointed at include the inability of subdomains to work with flask host or the need to make adjustment to the code below in my runserver.py for production environment of AWS.
if __name__ == '__main__':
HOST = environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)

Heroku Flask - Deploy a 'modular' app from tutorial not working, foreman start works locally

based on this structure: http://flask.pocoo.org/docs/patterns/packages/
I also tried this post: Deploying Flask app to Heroku
I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.
Here is my current project structure:
/myapplication
Procfile
runserver.py
/applicationfolder
__init__.py
views.py
Here is my Procfile
web: python runserver.py $PORT
Here is my runserver.py
from applicationfolder import app
app.run()
if __name__ == '__main__':
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Here is my init.py
import os
from flask import Flask
from flask import render_template, jsonify, request
app = Flask(__name__)
app.config.from_object('config')
import applicationfolder.views
From there views.py runs.
This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.
The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.
By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.
You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main, in which case don't start the service at module load time.

How to use mongohq or mongolab on OpenShift

I want to use mongoHQ in my application A part of code in views.py:
from pymongo import MongoClient
def connect_db():
client = MongoClient('mongodb://myname:mypassword#paulo.mongohq.com:10087/blog')
return client
#app.before_request
def before_request():
g.db = connect_db()
It's ok on localhost .
But it arise an HTTP 500 error on my browser when I deployed my app upon OpenShift.
(pymongo has been installed on OpenShift server.)
Anyone can help me
Thank you
There is a MongoHQ quickstart located here:
https://github.com/MongoHQ/mongohq-openshift-quickstart
While its a ruby app and not a python app it should point you in the right direction for your app config.
I had a similar problem earlier today. I solved it by adding the following line to requirements.txt:
pymongo==2.8.1

Categories