Exception running Python Flask app on AWS Elastic Beanstalk - python

I'm running into an issue trying to run my python web app on EB. It runs fine locally, but I get the following exception when I run it on EB. The problem seems to be that it runs into an exception, but the stack trace is of an error that occurs after that original one, so I can't really determine what the root cause is:
Exception on /admin GET
Traceback (most recent call last):
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functionshttp://rule.endpoint(**req.view_args)
File "/opt/python/run/venv/lib/python2.7/site-packages/sandman/sandman.py", line 459, in get_collection
cls = endpoint_class(collection)
File "/opt/python/run/venv/lib/python2.7/site-packages/sandman/sandman.py", line 185, in endpoint_class
cls = current_app.class_referencescollection
File "/opt/python/run/venv/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: 'Flask' object has no attribute 'class_references'
When I manually start the app on port 5000 after SSHing into my instance, and attempted to load the same page from the console, I did not get any error (and the saved file contained the correct HTML):
wget http://127.0.0.1:5000/admin
--2014-03-23 17:01:13-- http://127.0.0.1:5000/admin
Connecting to 127.0.0.1:5000... connected.
HTTP request sent, awaiting response... 127.0.0.1 - - 23/Mar/2014 17:01:13 "GET /admin HTTP/1.1" 301 -
301 MOVED PERMANENTLY
Location: http://127.0.0.1:5000/admin/ following
--2014-03-23 17:01:13-- http://127.0.0.1:5000/admin/
Connecting to 127.0.0.1:5000... connected.
HTTP request sent, awaiting response... 127.0.0.1 - - 23/Mar/2014 17:01:13 "GET /admin/ HTTP/1.1" 200 -
200 OK
Length: 1519 (1.5K) text/html
Saving to: ‘admin’
this is the python app code I'm running: http://pastebin.com/qgnWz6aK

I managed to work around this issue by using Tornado (though I'm still not sure what the underlying issue is):
from sandman import app
from sandman.model import activate
import logging
import os, sys
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DB']
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
activate(browser=False)
app.debug = True
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(os.environ['PORT'])
IOLoop.instance().start()

Related

python - blueprint declaration run with exceptions as I navigate it from a browser

I am using python 3.9.6 with ninja2 and blueprint.
py --version
returns:
Python 3.9.6
As far as I know, blueprint is a seperate folder, with seperate html templates and static files (css/js) , and I can have many blueprints in one runnable python project.
I have looked at Views with Jinja2 and blueprint
The hierarchy of the html+relevant files of myblueprint is:
main_project -> myblueprint -> templates -> myblueprint.html
-> static -> myblueprint.css
-> myblueprint.js
The relevant code:
import os
from flask import Flask, Blueprint, redirect, request, render_template, url_for, session
main_page = Blueprint('myblueprint', __name__)
#main_page.route("/myblueprint")
def home():
query = 'select * from users;'
users = interact_db(query=query, query_type='fetch')
return render_template('myblueprint.html')
...
#app.route('/', methods=['GET'])
def main():
return redirect("myblueprint")
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.secret_key = '12345'
app.run(host='0.0.0.0', port=port)
For some reason, when I run: https://localhost:5000, I get an error:
ERROR in app: Exception on /myblueprint [GET] Traceback (most recent
call last): File
"C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py",
line 2073, in wsgi_app
response = self.full_dispatch_request() File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py",
line 1518, in full_dispatch_request
rv = self.handle_user_exception(e) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py",
line 1516, in full_dispatch_request
rv = self.dispatch_request() File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py",
line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:/main_project/myproject/app.py", line 17, in home
return render_template('myblueprint.html') File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py",
line 148, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list), File
"C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py",
line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals) File
"C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py",
line 997, in get_template
return self._load_template(name, globals) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py",
line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals)) File
"C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\loaders.py",
line 125, in load
source, filename, uptodate = self.get_source(environment, name) File
"C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py",
line 59, in get_source
return self._get_source_fast(environment, template) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py",
line 95, in _get_source_fast
raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: myblueprint.html
127.0.0.1 - - [04/Jan/2022 08:04:31] "GET /myblueprint HTTP/1.1" 500 -
127.0.0.1 - - [04/Jan/2022 08:04:31] "GET /favicon.ico HTTP/1.1" 404 -
I have also noticed that on the chrome network console (not in code run window), I see another exception:
Request URL: http://localhost:5000/
Request Method: GET
Status Code: 500 INTERNAL SERVER ERROR
Remote Address: 127.0.0.1:5000
Referrer Policy: strict-origin-when-cross-origin
What is the cross origin for blueprint, and how can I avoid that?
What is wrong in my code above, and should I fix the heirarchy?
Two problems in your code. First you have to register your blueprint
import os
from flask import Flask, Blueprint, redirect, request, render_template, url_for, session
app = Flask(__name__)
main_page = Blueprint('myblueprint', __name__)
#main_page.route('/myblueprint', methods=['GET'])
def home():
query = 'select * from users;'
users = interact_db(query=query, query_type='fetch')
return render_template('myblueprint.html')
#app.route('/', methods=['GET'])
def main():
return redirect("myblueprint")
app.register_blueprint(main_page)
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5800))
app.secret_key = '12345'
app.run(host='0.0.0.0', port=port)
Then your html file must simply be in the templates folder:
> templates > myblueprint.html

Using Flask, Twilio API with PythonAnywhere gets error 11200 - HTTP retrieval failure

I made a flask app which detects messages received on whatsapp using Twilio Webhooks. After receiving a message, the app sends a message back to the same phone number. This works perfectly using Flask and Ngrok to deploy the server and expose it. Once I deploy it to PythonAnywhere however, I get a 11200 error in the Twilio console. Here's the code.
from flask import Flask, request
import requests
from twilio.rest import Client
account_sid = 'xxxxx'
auth_token = 'xxxxx'
client = Client(account_sid, auth_token)
def mmsg(phono, body):
message = client.messages.create(
from_='whatsapp:+14155238886',
body=body,
to=phono,
)
app = Flask(__name__)
#app.route('/post', methods=['POST'])
def bot():
incoming_msg = request.values.get('Body', '').lower()
phono = request.values.get('From', "")
if incoming_msg == 'hi':
mmsg(phono, 'hello!')
if __name__ == '__main__':
app.run()
When I check the PythonAnywhere error logs, I get this
2020-07-19 13:50:46,569: POST Request: https://api.twilio.com/2010-04-01/Accounts/AC84a8b5837227246efc0c6f9440b6e12c/Messages.json
2020-07-19 13:50:46,570: PAYLOAD: {'To': 'whatsapp:{myphonenumber}', 'From': 'whatsapp:+14155238886', 'Body': 'hello!'}
2020-07-19 13:50:49,576: Exception on /post [POST]
Traceback (most recent call last):
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.twilio.com', port=443): Max retries exceeded with url: /2010-04-01/Accounts/AC84a8b5837227246efc0c6f9440b6e12c/Messages.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fc0504362e8>: Failed to establish a new connection: [Errno 111] Connection refused',))
I have tried adding another key and value to the message = client.messages.create like this.
message = client.messages.create(
from_='whatsapp:+14155238886',
body=item,
to=phono,
AC84a8b5837227246efc0c6f9440b6e12c='83ce0b901ff353f9b9a77222e001d71d'
)
When I try that, I get this error on PythonAnywhere.
2020-07-19 14:09:51,030: Exception on /post [POST]
Traceback (most recent call last):
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/AbhayAysola/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/AbhayAysola/bot.py", line 46, in bot
mmsg(phono, ou)
File "/home/AbhayAysola/bot.py", line 24, in mmsg
AC84a8b5837227246efc0c6f9440b6e12c='83ce0b901ff353f9b9a77222e001d71d'
TypeError: create() got an unexpected keyword argument 'AC84a8b5837227246efc0c6f9440b6e12c'
Free accounts on PythonAnywhere have restricted Internet access; you can only access a specific set of sites (listed here, but this is a list of sites that you can access, not a list of sites that you can't access), and you have to use a proxy server to access them.
For most libraries that is completely transparent -- they pick up the proxy settings from the system environment and use it without you having to do anything extra. But the Twilio library needs a little bit of extra configuration. There's a help page explaining what this is here, but the specific change to your code would be to replace this:
client = Client(account_sid, auth_token)
...with this:
import os
from twilio.http.http_client import TwilioHttpClient
proxy_client = TwilioHttpClient(proxy={'http': os.environ['http_proxy'], 'https': os.environ['https_proxy']})
client = Client(account_sid, auth_token, http_client=proxy_client)

I am trying to run a simple flask application but i am having a TypeError [duplicate]

This question already has answers here:
Flask view return error "View function did not return a response"
(3 answers)
Closed 3 years ago.
I am trying to run a simple flask application in which it only prints hello world but i am having a type error. In the type error there is a trackback which is specifying a file named app.py and showing errors in it but i can't figure them out. The file in which i am having error is in the flask folder.Please help me that how i can resolve this problem.
I have already done running these code due to which the command prompt gives me the localhost code but when i tried to run it on the browser i am having the type error .
set FLASK_ENV=development
set FLASK_APP=app.py
This is my code
from flask import Flask
app = Flask("__name__")
#app.route("/")
def index():
print("Hello World")
When i run these three lines on my command prompt
set FLASK_ENV=development
set FLASK_APP=app.py
flask run
**i receive this **
Serving Flask app "app.py" (lazy loading)
Environment: development
Debug mode: on
Restarting with stat
Debugger is active!
Debugger PIN: 179-513-092
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
after this when i use the code in my browser the browser shows me this
Traceback (most recent call last):
File "c:\users\muhammad umar\appdata\local\programs\python\python37- 32\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "c:\users\muhammad umar\appdata\local\programs\python\python37- 32\lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "c:\users\muhammad umar\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "c:\users\muhammad umar\appdata\local\programs\python\python37-32\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "c:\users\muhammad umar\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\muhammad umar\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
File "c:\users\muhammad umar\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1967, in finalize_request
response = self.make_response(rv)
File "c:\users\muhammad umar\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2097, in make_response
"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
You're not supposed to print the response, you need to return it, so that Flask can send it to the browser:
from flask import Flask
app = Flask("__name__")
#app.route("/")
def index():
return "Hello World"
This is not to say that you cannot print, you can (it shows up in the console), but remember to return a response in your routes.
You don't need to 'print' the output but need to 'return' it. So changed code will be :
from flask import Flask
app = Flask("__name__")
#app.route("/")
def index():
return "Hello World"

Python Flask web app hanging

I'm very new to python programming, I'm trying to expose few terminal commands as a http call. I'm packaging this below code into a tar.gz using setup.py and extracting this on the server to run the code in nginx web server. once I instantiate the app.py code using below, the http calls work fine for few hours. Not sure what happens after that, the http pages show an error and I do see the app is running in the server. Can someone please help me with this
gunicorn --bind 0.0.0.0:8000 cstaradmin:app &
#!/usr/bin/env python
from flask import Flask
from shelljob import proc
from flask import Response
from flask import json,jsonify
def get_pretty_print(json_object):
return json.dumps(json_object, sort_keys=False, indent=4, separators=('\n',' '))
def read_process(g):
while g.is_pending():
lines=g.readlines()
for proc,line in lines:
yield line
app = Flask(__name__)
def has_no_empty_params(rule):
defaults = rule.defaults if rule.defaults is not None else ()
arguments = rule.arguments if rule.arguments is not None else ()
return len(defaults) >= len(arguments)
#app.route('/', methods = ['GET'])
def help():
"""Print available functions."""
func_list = {}
for rule in app.url_map.iter_rules():
if rule.endpoint != 'static':
func_list[rule.rule] = app.view_functions[rule.endpoint].__doc__
return jsonify(func_list)
#app.route("/ps")
def IRR():
IRR = ["ps"]
g=proc.Group()
p=g.run(IRR)
return Response(read_process(g),mimetype= 'text/plain')
def catch_all(path):
return
if __name__ == "__main__":
try:
app.run(host='0.0.0.0',port=8000)
except:
pass
Error:
Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application
[2016-10-22 15:38:55,505] ERROR in app: Exception on /path/ToMy/command [GET]
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/path/ToMyapp/admin/cstaradmin.py", line 42, in listnodes
p=g.run(listnodes)
File "/usr/lib/python2.7/site-packages/shelljob/proc.py", line 53, in run
CommandException( e, "Group.run '{}' failed".format( cmd ) ).do_raise()
File "/usr/lib/python2.7/site-packages/shelljob/proc.py", line 51, in run
return self._run_impl( cmd, shell )
File "/usr/lib/python2.7/site-packages/shelljob/proc.py", line 63, in _run_impl
stdin = subprocess.PIPE, # needed to detach from calling terminal (other wacky things can happen)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1216, in _execute_child
errpipe_read, errpipe_write = self.pipe_cloexec()
File "/usr/lib64/python2.7/subprocess.py", line 1168, in pipe_cloexec
r, w = os.pipe()
CommandException: ("Group.run '['nodetool', ' status']' failed", OSError(24, 'Too many open files'))

Authentication Error when using Flask to connect to ParseServer

What I am trying to achieve is pretty simple.
I want to use Flask to create a web app that connects to a remote Server via API calls (specifically ParseServer).
I am using a third-party library to achieve this and everything works perfectly when I am running my code in a stand-alone script. But when I add my code into the Flask I suddenly can't authenticate with the Server
Or to be more precise I get an 'unauthorized' error when executing an API call.
It seems to me that in Flask, the registration method used by the APi library is not remembered.
I tried many things of putting the registration and initialization code in different places in Flask, nothing worked.
I asked a similar question in the Github of the Library with no help.
So I guess I have two questions that could help me solve this
1) Where should I put a registration method and import of the files from this library?
&
2) What can I do to identify the issue specifically, eg. to know precisely what's wrong?
Here's some code
The Flask code is here
#app.route('/parseinsert')
def run_parse_db_insert():
"""The method for testing implementation and design of the Parse Db
"""
pc = ParseCommunication()
print(pc.get_all_names_rating_table())
return 'done'
The ParseCommunication is my Class that deals with Parse. If I run ParseCommunication from that script, with the same code as above in the main part, everything works perfectly.
I run the Flask app with dev_appserver.py from Google App Engine.
My folder structure
/parseTest
/aplication
views.py
app.yaml
run.py
My run.py code
import os
import sys
sys.path.insert(1, os.path.join(os.path.abspath('.'), 'lib'))
sys.path.insert(1, os.path.join(os.path.abspath('.'), 'application'))
import aplication
Let me know what else I could provide to help out.
Thank you in Advance
EDIT:
A stack trace as requested.
It's mostly related to the library (from what I can say?)
ERROR 2016-09-28 06:45:50,271 app.py:1587] Exception on /parseinsert [GET]
Traceback (most recent call last):
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/theshade/Devel/ParseBabynames/parseTest/aplication/views.py", line 34, in run_parse_db_insert
name = pc.get_user('testuser1')
File "/home/theshade/Devel/ParseBabynames/parseTest/aplication/parseCommunication.py", line 260, in get_user
return User.Query.get(username=uname)
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/parse_rest/query.py", line 58, in get
return self.filter(**kw).get()
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/parse_rest/query.py", line 150, in get
results = self._fetch()
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/parse_rest/query.py", line 117, in _fetch
return self._manager._fetch(**options)
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/parse_rest/query.py", line 41, in _fetch
return [klass(**it) for it in klass.GET(uri, **kw).get('results')]
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/parse_rest/connection.py", line 108, in GET
return cls.execute(uri, 'GET', **kw)
File "/home/theshade/Devel/ParseBabynames/parseTest/lib/parse_rest/connection.py", line 102, in execute
raise exc(e.read())
ResourceRequestLoginRequired: {"error":"unauthorized"}
Parse requires keys and env variables. Check this line:
API_ROOT = os.environ.get('PARSE_API_ROOT') or 'https://api.parse.com/1'
Your error is in line 102 at:
https://github.com/milesrichardson/ParsePy/blob/master/parse_rest/connection.py
Before you can parse, you need to register:
from parse_rest.connection import register
APPLICATION_ID = '...'
REST_API_KEY = '...'
MASTER_KEY = '...'
register(APPLICATION_ID, REST_API_KEY, master_key=MASTER_KEY)

Categories

Resources