Python App Engine : use app.yaml to control url handler - python

When I control different type of pages, I move my code to another python file. But this way has disadvantage : each time I want to change url hander, I must comback to main.py to config bottom lines about url handler. for example :
app = webapp2.WSGIApplication([('/', MainPage),
('/thanks',ThanksHandler),
('/unit2/signup',Signup),
('/unit2/successful', LoginSuccess)], debug=True)
I try to config handler in app.yaml to prevent dis advantage.
I add file blog.py in same directory and in this file, I have Blog class. And here is my blog.py file:
class Blog(BaseHandler):
def get(self):
self.response.out.write("Hello")
app = webapp2.WSGIApplication([('/blog', Blog)], debug=True)
Here is original file:
> handlers:
> - url: /favicon\.ico static_files: favicon.ico upload: favicon\.ico
- url: /.* script: main.app
and this new file app.yaml:
handlers:
- url: /favicon\.ico static_files: favicon.ico upload: favicon\.ico
- url: /blog/.* script: blog.app
- url: /.* script: main.app
But when I goto: localhost:port/blog : 404: resource not found.
Please help me.
Thanks :)

The /blog/.* url specification from the yaml file does not match the url specification from the blog.py file (/blog). In particular the fact that /blog/.* requires the url to have a slash after blog. If for example you use just /blog in both places it will work. Or you can use /blog/.* in both places.
The url specifiers are matched in the order in which they appear in the yaml file therefore in this particular case /blog/.* will not match on /blog but will match on the last (catch all really) /.* specifier and therefore main.py handler will be loaded and fail to match (no pattern in the call WSGIApplication constructor inside main.py).
Hope this helps.
-Silviu

Related

Requested url not found on server error

I made a website which I am temporarily hosting in app engine. I am able to navigate through the pages when I open the HTML files on my computer. It only fails when I head to http://www.alshafarconstruction.appspot.com/index.html.
Error Message:
Error: Not Found
The requested URL /contact.html was not found on this server
app.yaml:
application: alshafarnew
version: 1
runtime: python
api_version: 1
handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css|swf|xml))
static_files: \1
upload: (.*\.(gif|png|jpg|ico|js|css|swf|xml))
- url: /(.*\.html)
static_files: \1
upload: index.html
- url: /.*
script: main.py
main.py:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class IndexHandler(webapp.RequestHandler):
def get(self):
if self.request.url.endswith('/'):
path = '%sindex.html'%self.request.url
self.redirect(path)
def post(self):
self.get()
application = webapp.WSGIApplication([('/.*', IndexHandler)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
I've checked the dashboard and find that the HTML files are there. I am new to website deployment and design. Can anyone suggest possible errors? I have already visited many sites for a solution.
The documentation hints that static and application files are uploaded separately, so while the files may be there, it might be that they're being uploaded in the application-files part.
You currently have an upload directive telling it to only upload index.html:
- url: /(.*\.html)
static_files: \1
upload: index.html
(actually, it's a regular expression, so it would also upload, for example index#html if it existed)
This suggests that you might want to make that regular expression match all HTML files:
- url: /(.*\.html)
static_files: \1
upload: .*\.html

GAE app.yaml Script Handling for scripts in subdirectories (python)

I am setting up a new GAE project, and I can't get my scripts in sub-directories to work.
EDIT: If I go to localhost:8080/testing_desc.html I don't any errors, just a blank page (view-source is blank, too). Scripts in my root dir work normally. There is a __init__.py in both the root and sub-dir.
Python script example ("/testing/testing_desc.py"):
import webapp2 as webapp
class DescTstPage(webapp.RequestHandler):
def get(self):
html = 'This should work.'
self.response.out.write(html)
app = webapp.WSGIApplication([('/.*', DescTstPage)], debug=True)
app.yaml:
application: blah
version: blah
runtime: python27
api_version: 1
default_expiration: "5d 12h"
threadsafe: false
libraries:
- name: webapp2
version: latest
handlers:
- url: / <-- This one works
script: main.app
- url: /index.html <-- This does NOT work (??)
script: main.app
- url: /(.*?)_desc.html <-- Also does NOT work
script: \1/\1_desc.app
#file not found
- url: /.*
script: file_not_found.app
I have also tried a simpler version of the yaml:
-url: /testing_desc.html
script: /testing/testing_desc.app
When you use WSGI Pyhon27 this will work. Use a dot for the seperator :
-url: /testing_desc.html
script: /testing.testing_desc.app
You are passing a list of routes to your WSGIApplicaiton call. Those routes must match whatever URL is being handled.
('/', DescTstPage) just matches the '/' URL to your handler. /.* matches all routes.
What routes are you setting in testing/testing_desc.app? They must match /testing_desc.html.
To get my scripts working in sub-dir, I changed the app.yaml and /testing/testing_desc.py to this:
app.yaml:
- url: /testing.html
script: testing/testing_desc.py
/testing/testing_desc.py:
app = webapp.WSGIApplication([('/.*', DescTstPage),], debug=True)
def main():
run_wsgi_app(app)
if __name__ == '__main__':
main()
ATM I do not understand how to make the routing work with sub-dir, so I will use this.
The answer is here:
How can i use script in sub folders on gae?
Its not explicitly stated, but in the answer you must change /foo/ to /foo in the WSGIApplication call, which maps to www.bar.com/foo. If you want to map to www.bar.com/foo.html you need to change /foo to /foo.* in the WSGIApplication call AND the url handler in app.yaml to - url: /foo\.html

To host a static (HTML) website on Google App Engine, what should be in the app.yaml file?

Can I use this for uploading HTML pages?
app.yaml contents:
application: visualvidya
version: 1
runtime: python
api_version: 1
handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css))
static_files: \1
upload: (.*\.(gif|png|jpg|ico|js|css))
A minimal handlers section for a static site might look something like this:
handlers:
- url: /
static_files: static/index.html
upload: static/index.html
- url: /
static_dir: static
Every site is different, so as Sean pointed out in the comments, you'll want to consult the documentation.

Specific all static files which is same directory level as app.yaml

I have all static files same directory as my app.yaml
Now, I have to specific the static files one by one in my app.yaml
application: xxx
version: 1
runtime: python
api_version: 1
handlers:
- url: /
static_files: index.html
upload: index.html
- url: /style.css
static_files: style.css
upload: style.css
- url: /android.png
static_files: android.png
upload: android.png
- url: /logo.png
static_files: logo.png
upload: logo.png
- url: /helloworld.py
script: helloworld.py
Is there any easier way for me to specific all my static files? (Provide they are same directory level as app.yaml)
I try
- url: /
static_files: index.html
static_dir: \.
upload: index.html
It won't work.
Fatal error when loading application configuration:
Unexpected attribute "static_dir" for mapping type static_files.
in "C:\Projects\xxx-website\app.yaml", line 12, column 1
As others suggested you can put your files in the static folder and have an app.yaml that looks like this (so you don't have to enter manually every file):
https://gist.github.com/3031499
You can also specific handlers at the top like:
- url: /counter
script: counter.app
- url: /(.*\.rar)
script: download.app
Any reason to try and do this? Place them all in the static directory.

How to migrate my app.yaml to 2.7?

I'm migrating my gae app to python 2.7. This is my new app.yaml:
application: webfaze
version: main
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /mapreduce(/.*)?
script: mapreduce/main.application
- url: /(.*\.(html|css|js|gif|jpg|png|ico|swf))
static_files: static/\1
upload: static/.*
expiration: "1d"
- url: .*
script: main.application
- url: /task/.*
script: main.application
login: admin
But I get this error message:
Error parsing yaml file:
Invalid object:
threadsafe cannot be enabled with CGI handler: mapreduce/main.application
in "webfaze/app.yaml", line 22, column 1
Can you tell me how to resolve the error?
Checking the source code, it looks that you need to define your handlers' path without any slash:
if (handler.script and (handler.script.endswith('.py') or
'/' in handler.script)):
raise appinfo_errors.ThreadsafeWithCgiHandler(
'threadsafe cannot be enabled with CGI handler: %s' %
handler.script)
Move application.py to the root of your project and modify the handler's path accordingly.
Change:
- url: /mapreduce(/.*)?
script: mapreduce/main.application
To:
- url: /mapreduce(/.*)?
script: mapreduce.main.application
You may also need to add an __init__.py to the 'mapreduce' folder if one doesn't exist there already. That will make the python interpret the folder as a module.

Categories

Resources