Getting internal server error on Flask App hosted on Azure - python

I've hosted a flask app on Azure, but there seems to be some problem with linking the WSGIHandler. It is a very simple bug. I can't seem to identify it.
The following is the error I'm getting in my Logs
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "D:\Python27\Scripts\wfastcgi.py", line 711, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\Python27\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
return env, get_wsgi_handler(handler_name)
File "D:\Python27\Scripts\wfastcgi.py", line 551, in get_wsgi_handler
raise ValueError('"%s" could not be imported' % handler_name)
ValueError: "App" could not be imported
StdOut:
StdErr:
ErrorCode Access is denied.
(0x5)
Here is my folder structure
myapplication
-- App
-- __init__.py
The contents of __init__.py is
from flask import Flask
# initialize the flask app
app = Flask(__name__)
print "init"
#app.route('/')
def hello():
return "hello world";
if __name__ == "__main__":
app.run()
I've configured the following App Settings in Azure Web App
PYTHONPATH = D:\home\site\wwwroot
WSGI_HANDLER = App.app

Per my understanding, your deployment is incompleteness, as Azure uses IIS to host python web sites on Web Apps Services, which needs a web.config to configure hander mapping and URL rewrite rules and some other settings.
To create and deploy a flask project on Azure Web Apps , we can generally follow the steps below:
1, On Azure manage portal, click NEW => COMPUTE => WEB APP =>FROM GALLERY at the bottom navigation, on the ADD WEB APP dialog, select Flask, name the site on the next dialog page. Now we have created a flask web site project.
We can type the endpoint on the browser to check the website, http://<your_site_name>.azurewebsite.net
2,On the web apps list, click the name we created above to get into the configuration page, click DASHBOARD, at the quick glance column, click Set up deployment from source control ,select Local Git repository. Now there is an additional tab named DEPLOYMENT beside the DASHBOARD tab. And in the DEPLOYMENT page there are steps of how to deploy your site by git. We can clone the project to local by the GIT URL provided on this page.
We can get more on this official article

Related

Unknown FastCGI error Occurred python web API

I am using python version 3.8 and IIS 7.0. When I try to host my python web api on the IIS server it encounter with the FastCGI error. I have enable CGI in IIS and also added System.WebServer>>handlers>>Python FastCGI in my web config but still it gives same error. I have also checked the wfastcgi and flask are also successfully added.
You had to put both parameters in double quote separated by |
e.g.
"c:\python39\python.exe" | "c:\python39\Lib\site-packages\wfastcgi.py"
or put the whole path in "" after pasting it.
follow below steps to configure iis flask app in iis:
1)install python
2)after installing python install the wfastcgi. run the command prompt as administrator and run below command:
pip install wfastcgi
wfastcgi-enable
3)below is my flask example:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
app.run()
4)enable the cgi feature of iis:
5)open iis create a site.
6)after adding site select the site name and select the handler mapping feature from the middle pane.
Click “Add Module Mapping”
executable path value:
C:\Python37-32\python.exe|C:\Python37-32\Lib\site-packages\wfastcgi.py
Click “Request Restrictions”. Make sure “Invoke handler only if the request is mapped to:” checkbox is unchecked:
Click “Yes” here:
7)now go back and select the application setting feature.
click add from the action pane.
Set the PYTHONPATH variable(which is your site folder path):
And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app — if yours is named site.py it would be site.app or similar):
8)Click OK and browse to your site.
Note: Do not forget to assign the iis_iusrs and iusr permission to the site folder and the python folder.

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

Error while deploying flask app to Elastic Beanstalk

I am trying to deploy a Flask app on AWS Elastic Beanstalk through the console GUI. Following the answer given here Your WSGIPath refers to a file that does not exist,
I have named my main file application.py and have set the WSGIPath (through the Beanstalk console GUI) to application.py. Also, I have named my object application, not app. However, I am still getting this error:
Your WSGIPath refers to a file that does not exist.
Here is my file structure:
Here is the error that shows up
How can I resolve this error?

git webhook fails - do you know why?

Background:
I have a bitbucket repo called DOSTUFF that includes a python script do_stuff.py. I edit it using Eclipse pydev on my local machine and push changes to bitbucket via git push origin master.
I cloned DOSTUFF to a pythonanywhere trial account without any issues.
Now, whenever I edit do_stuff.py locally and then git commit -m 'foo' & git push origin master them to bitbucket, I manually need to git pull from within pythonanywhere afterwards in order to see the edits in pythonanywhere. This is inefficient.
Objectives:
I want that my local (Eclipse) commits to bitbucket get pulled automatically to pythonanywhere once pushed from local to bitbucket. Apparently, webhooks are the way to go.
Challenges:
In order to do so, I followed this hint by specifying a webhook within bitbucket to pythonanywhere/user/me/webhook.py. Unfortunately, those instructions are minimalistic as they lack proper imports and don't elucidate as to why flask is necessary (I am not an expert).
webhook.py looks like this:
#!/usr/bin/python2.7
# -*- coding: utf-8 -*
import git
from flask import Flask, request
# Initiate flask instance
app = Flask(__name__)
#app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
repo = git.Repo('./DOSTUFF')
origin = repo.remotes.origin
repo.create_head('master',
origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
origin.pull()
return '', 200
else:
return '', 400
if __name__ == '__main__':
app.run(port=5000,debug=True)
Now, when I git push from Eclipse to bitbucket, the commit(s) arrive at bitbucket but the code in pythonanywhere remains unchanged. In other words, webhook.py fails.
In contrast, when I run webhook.py from within pythonanywhere (bash console), I yield the following error:
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
Traceback (most recent call last):
File "/home/ME/webhook.py", line 21, in <module>
app.run(port=5000,debug=True)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 795, in run_simple
s.bind(get_sockaddr(hostname, port, address_family))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use
Questions:
What's the root cause for this to fail?
How to properly configure a webhook that is necessary and sufficient to auto-git pull changes to pythonanywhere once pushed from local to bitbucket?
You're trying to start a server in a PythonAnywhere console, which will not work since traffic is not routed to console servers. Use a web app to create the server to listen for the web hooks.

Changing a file on Google App Engine

I have a Google App Engine Python project that I haven't touched since 2014, when you worked with a desktop app to upload and deploy it. Google App engine changed a lot in the meantime. According to the information the deploying guide I copy
the file(s) like this:
gsutil cp main.py gs://myproject.appspot.com/
I converted my app.yaml file to a json file using their utility. When I initially deployed the app using this page it failed because there was no deployment information, so I added this:
"deployment": {
"files": {
"main": {
"sourceUrl": "https://storage.googleapis.com/myproject/main.py"
},
}
},
However when I deployed again I got an error because it couldn't find the file with that URL. The gsutil has to use a bucket with gs://something and the file has to exist at https:// something. How can I upload the file to the project and specify the location in the app.json file?
You were probably using what is now called App Engine Standard environment. Deploys are handled with the gcloud app deploy command.
Example:
gcloud app deploy --version [YOUR_VERSION_ID] --no-promote --project [YOUR_PROJECT_ID]
Here is more info for Deploying a Python App
The deploy command uses app.yaml file for configuration info.

Categories