Python Flask "The configuration file does not exist." accessing sub-path - python

I am deploying Flask on CPanel, and basic configurations are working correctly,
up to the point that I can access home route, defined as:
#app.route("/home")
#app.route("/")
def home():
return "This is home page, blah blah blah"
The site can be thought of, as being accessed at
https://sub-domain.main-domain.com/base-url,
thus, home page is accessed as https://sub-domain.main-domain.com/base-url/ and that works perfectly.
Problem
So, the issue comes when I access any other route, other than the slash("/"),
in the above example, for instance, accessing
https://sub-domain.main-domain.com/base-url/home,
doesn't seem to work at all, and results in
The configuration file does not exist. error.
Any possible help would be kindly appreciated. my passenger_wsgi.py is configured as:
import sys
# add your project directory to the sys.path
base_dir = u'/home/cpaneusername/base-dir'
if base_dir not in sys.path:
sys.path = [base_dir] + sys.path
# import flask app but need to call it "application" for WSGI to work
from app import website as application
Nevertheless, thanks for the assistance.

What I can suggest resolving the issue is to change the base URL of your Python app and remove any sub-folders from it. For example, just keep it that way:
https://sub-domain.main-domain.com/
And try again.
Otherwise, you might want to change the /home route of your app to this one instead:
#app.route("/base-url/home")
If none of the above works, then make sure that you have the correct .htaccess rules in place to forward the requests properly to your app, but I assume you already have considering the fact that you are able to access the / of the app.

I tried moving the Python app and other supporting modules to /public folder, and in the wsgi file, I imported as
from public.app import website as application
There, it worked.

Related

How to serve static files WebApp2 NO Google AppEngine

I am trying to use WebApp2 outside of AppEngine and can't find anywhere in the documentation how to set up static routes to files.
For example, I have the following folder structure
Presentation
-->js
-->-->main.js
-->templates
-->-->index.html (loaded via Jinja)
How do I reference the main.js as using ../js/main.js gives me a 404
Here's the documentation that deals with this: Quick start (to use webapp2 outside of App Engine). Did this not work?
Serving static files should occur outside of your WSGI application (the webapp2 app), so it depends on what server you are using to run your webapp2 app (which is a WSGI app), because that server will need to be configured to serve the static files. Could you please add to your question what kind of server you are using, and/or how you are running the webapp2 app?
Appreciate your response, found similar after much digging. Problem was my lack of understanding of exactly what WebApp2 was as normal point AppEngine Dev App Server at it.
I only need a simple HTTP server, so doing the following
static_app = StaticURLParser("Presentation/")
# Create a cascade that looks for static files first, then tries the webapp
app = Cascade([static_app, web_app])
def main():
httpserver.serve(app, host='127.0.0.1', port='8080')
if __name__ == '__main__':
main()
and then run python main.py

Server Error in Default Deployment of Azure Flask Web App

I'm trying to deploy a Flask app within Azure, using the pre made template created by Azure.
I can't get a simple...
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run()
To do anything. To simplify further still, even the pre made deployment returns an error...
Clearly I'm misunderstanding something very fundamental here.
Using FTP I can see the files that Azure is creating - sure does look like there should be a nice little demo site.
What am I doing wrong?
Cheers, Ben
According to your description, I think the issue was caused by some incorrect configuration in the web.config & the Application Settings of your WebApp settings. Please try to refer to the blog to resolve it.
Meanwhile, I don't know what pre made template created by Azure you used. I tried to create a new Azure WebApp for Flask as below.
The file list in the wwwroot path in the Kudu tool or FTP shown as below.
Then, I modified the code views.py in the FlaskWebProject1 directory. And it works.
It seems to be more simple. Hope it helps.
I've been figthing the same issue for a while, I have only be able to workaround it by changing the pricing tier associated to the "service plan" to S2 Standard. If I try to use S1 Standard I run into the same issues.

Catch-all URL with Flask when the path also contains a URL

Ok, so I want to catch all the URLs in my Flask app, with this piece of code
#app.route('/<path:path>')
def catch_all(path):
return path
Just a basic app with shows the relative path of each page. So this works fine for most of the paths (localhost/whatever/I/want for example prints whatever/I/want).
EXCEPT in one case. For example if I type localhost/foo/http://google.com/bar in the URL bar, what I expected to be shown is foo/http://google.com/bar, but what I actually get is only bar. So actually Flask finds the last valid URL and takes the path following this URL.
Is there anyway to catch the real path of our URL?
Edit: I'm running Flask 0.10 using the Google App Engine for Python.
I was able to reproduce your problem with the current Google App Engine SDK; this is caused by the way the bundled CherryPy server parses the request.
The problem applies only to your local dev server, when you deploy the app to Google, a different handler parses the URI and the http:// scheme is left unparsed.
See CherryPy issue #1284 as well as the corresponding appengine devserver ticket.
The work-around is to URI-encode the colon:
http://localhost:8000/foo/http%3A//google.com/bar
works.

Google App Engine Application Cache

In google app engine i have created my own user API appropriately called user so it doesn't interfere with the google app engine API users. Like most multiuser websites, two "versions" of the site are available to the user depending on whether or not they are logged in. Thus is created a file called router.py with the following code
import webapp2
from lib import user
import guest
import authorized
if user.isLoggedIn():
app = webapp2.WSGIApplication(authorized.WSGIHandler,debug=True)
else:
app = webapp2.WSGIApplication(guest.WSGIHandler,debug=True)
the guest and authorized modules are formated like your conventional application script for example:
import webapp2
import os
class MainPage(webapp2.RequestHandler):
def get(self,_random):
self.response.out.write('authorized: '+_random)
WSGIHandler = [('/(.*)', MainPage)]
Thus the router file easily selects which WSGIApplication url director to use by grabbing the WSGIHandler variable from either the guest or authorized module. However the user must close all tabs for the router to detect a change in the isLoggedIn() function. If you log in it does not recognize that you have done so until every tab is closed. I have two possible reasons for this:
isLoggedIn() uses os.environ['HTTP_COOKIE'] to retrieve cookies and see if a user is logged in, it then checks the cookie data against the database to make sure they are valid cookie. Possibly this could have an error where the cookies on the server's end aren't being refreshed when the page is? Maybe because i'm not getting the cookies from self.request.
Is it possible that in order to conserve frontend hours or something that google app engine caches the scripts from the server with in the server's memcache? i Doubt it but i am at a loss for the reason for this behavior.
Thanks in advance for the help
Edit
Upon more testing i found that as suspected the router.py file responded correctly and directed the user based in logged in when a comment was added to it. This seems to indicate caching.
Edit 2
I have uncovered some more information on the WSHI Application:
The Python runtime environment caches imported modules between requests on a single web server, similar to how a standalone Python application loads a module only once even if the module is imported by multiple files. Since WSGI handlers are modules, they are cached between requests. CGI handler scripts are only cached if they provide a main() routine; otherwise, the CGI handler script is loaded for every request.
I wonder how efficient to refresh the WSGI module somehow. This would undoubtably task the server, but solve my problem. Again, this seems to be a partial solution.
Edit 3
Again, any attempt to randomize a comment in the router.py file is ineffective. The id statement looking for user login is completely overlooked and the WSGIApplication is set to its original state. I'm not yet sure if this is due to the module cache in the webapp2 module or thanks to the module cache on the user API. I suspect the latter.
The problem is not "caching", it is just how WSGI applications work. A WSGI process stays alive for a reasonably long period of time, and serves multiple requests during that period. The app is defined when that process is started up, and does not change until the process is renewed. You should not try to do anything dynamic or request-dependent at that point.
replace router.py with:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from lib import user
import guest
import authorized
def main():
if user.isLoggedIn():
run_wsgi_app(authorized.application)
else:
run_wsgi_app(guest.application)
if __name__ == "__main__":
main()
downgrading to the old webapp allows you to change dynamically the wsgi application. it's tested and works perfectly! The CGI adaptor run_wsgi_app allows for the webapp to change it's directory list without caching.

How to deploy Django application at sub-url?

I need to set up a django development environment that is publicly viewable on the internet (I am doing this for school, and my projects need to be viewable by my professor, this isn't a setup that needs much security). I have a virtual server running Ubuntu 8.04 LTS.
I need to have multiple django applications running in subdirectories of my main site. That is, I need mysite.com to be a static page, mysite.com/wordpress to be my wordpress blog, and mysite.com/django1 mysite.com/django2 etc. to be django projects.
I am using apache, and I will either be using sqlite or mysql.
There seem to be as many different ways to install and configure django as there are websites offering advice, and all of them assume a single project is going to be the root of the website. I'd really appreciate some help, thank you.
You can use
WSGIScriptAlias /django1 /home/keratacon/www/django1/wsgi.py
WSGIScriptAlias /django2 /home/keratacon/www/django2/wsgi.py
in your apache+mod_wsgi config, assuming wsgi.py is the name of your wsgi script.
This blog explains the solution (assuming that mod_wsgi is used, with nginx/uwsgi the solution is similar apparently in nginx/uwsgi this is not necessary).
The first parameter of WSGIScriptAlias - the /sub-url will be stripped from the request url and the rest will go to your django app. If your Django app urls all start with /sub-url (which are stripped by mod_wsgi), then you will not be able to show the views at those urls, unless you "re-insert" the /sub-url to the request path part.
import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
#the line below will re-append the sub-url to all urls
environ['PATH_INFO'] = environ['SCRIPT_NAME'] + environ['PATH_INFO']
#this one will make django think that it's the only thing running on the server
environ['SCRIPT_NAME'] = '' # my little addition to make it work
return _application(environ, start_response)
Also in your urls.py all urls must be prefixed with the sub-url of your interest.
Finally, the WSGIScriptAlias must be the same as your sub-url:
#the below line will "take-out" the sub-url and pass the rest
#to your wsgi script
WSGIScriptAlias /sub-url /path/to/wsgi_script
Where file /path/to/wsgi_script must contain the definition of application as shown in the first code snippet.
To make the "sub-url" setup explicit in Django, the equivalent request path patching would have to occur within the Django framework.

Categories

Resources