webapp2 routing can't route to handler - error 404 - python

For a blog project, I'm trying to set different webapp2 handlers for different urls. One of them is the "permalink" url of a post (accessed by post id). Another one is the url for deleting said post. When I try to go to such url, I get a blank page, and the AppEngineLauncher console says:
INFO 2014-01-20 08:08:42,574 module.py:612] default: "GET /del/5066549580791808 HTTP/1.1" 404 -
This is the code for the handlers part of my program:
application = webapp2.WSGIApplication([ ('/newpost', NewPost), #works OK
('/([0-9]+)', PermaLink), #works OK
('/del/([0-9]+)', Delete), #won't work!!!
('/', Front)], debug=True) #works OK
If somebody has some clue about this I'd appreciate it. I've been looking for a solution but the fact that I get no error message and it doesn't seem (to me at least) to make any sense makes it so much harder.
EDIT:
The app.yaml file:
application: blogapp
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
static_dir: static
- url: /.*
script: base.application
libraries:
- name: jinja2
version: latest
The Delete class is trivial code for testing, such as:
class Delete(Base): #Base is my base RequestHandler
def get(self, s):
self.response.write(s)
I even tried matching the urls '/del/([0-9]+)' to the same PermaLink class, and still doesn't work.

Nevermind, it's solved. I tidied up the yaml file and everything works correctly now.
application: blogapp
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /static
static_dir: static
- url: (/.*)*
script: base.application
libraries:
- name: jinja2
version: latest

Related

"POST /_ah/mail/.+" returning 404

I have been reading on stack overflow and but so far I have not found a solution that would work for me. I want to be able to handle incoming email to my app and eventually work with attachments but I am facing an issue.
app.yaml
application: egg-api
runtime: python27
api_version: 1
threadsafe: yes
builtins:
- remote_api: on
# Activate email receiving
inbound_services:
- mail
# This handler tells app engine how to route requests to a WSGI application.
- url: .* # This regex directs all routes to main.app
script: main.app
- url: /_ah/mail/info#egg-api.appspotmail.com # route everything to handle incoming
script: handle_incoming_email.app
login: admin
# Third party libraries
libraries:
- name: jinja2
version: latest
- name: lxml
version: latest
- name: webapp2
version: latest
- name: MySQLdb
version: latest
handle_incoming_email.py
import logging
import webapp2
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.api import mail
class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
logging.info("Received a message from: " + mail_message.sender)
app = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)
After starting dev_appserver successfully:
From http://localhost:8000/mail I try to send an email to info#egg-api.appspotmail.com but I get:
INFO 2016-04-06 21:31:26,549 module.py:787] default: "POST /_ah/mail/info%40egg-api.appspotmail.com HTTP/1.1" 404 106
When I visit http://localhost:8080/_ah/login I have no clue what I am supposed to enter.
Thanks for any help
Move the general handler
- url: .*
to after the specific handler
- url: /_ah/mail/info#egg-api.appspotmail.com
ie:
- url: /_ah/mail/info#egg-api.appspotmail.com # route everything to handle incoming
script: handle_incoming_email.app
login: admin
- url: .* # This regex directs all routes to main.app
script: main.app
Otherwise the general URL rule is applied first, resulting in a 404.

Unable to add Image on Google App Engine Website

I am Trying to make a simple website with a image in it by using google app Engine. I am unable to make it.
My App.yaml code is below:
application: simplegraph-007
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /image/.*
static_dir: static/image/.*
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
My Main.py looks like:
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("<img src ='/image/Ris.jpeg'/>")
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
My log is here:
INFO 2015-10-19 18:46:39,111 module.py:786] default: "GET /image/Ris.jpeg HTTP/1.1" 404 154
I hope to have help from you.
Thanks,
You can serve static images like this.
In app.yaml
- url: /image
static_dir: image
In your app root folder, create a folder /image and put Ris.jpeg there.
Folder layout should look similar to this
+ image
|
-- Ris.jpeg
- app.yaml
- main.py
GET /image/Ris.jpeg will now return the image file.
Also see the docs.
Check your logs when you start your app locally. I am sure it is printing an error. This is invalid for static_dir
- url: /image/.*
static_dir: static/image/.*
It does not need a wildcard. It should read:
- url: /image
static_dir: static/image
Your image in your local filesystem should be static/image/Ris.jpeg.

My google app engine website is only displaying my main page after migrating to python 2.7

Can anyone give me some pointers on how to get the other pages to show?
I this is my main.py
import webapp2
import os
import jinja2
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__))))
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.write(template.render())
application = webapp2.WSGIApplication([
('/',MainPage),
], debug=True)
And this is my app.yaml
application: ftmyersptcong
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: jinja2
version: latest
handlers:
- url: /styles
static_dir: styles
- url: /images
static_dir: images
- url: /scripts
static_dir: scripts
- url: /.*
script: main.application
I keep getting a 404 Not Found Page when I to click on any of the links to other pages which have been href'd in my html code.
This is what is in the log:
INFO 2014-11-12 18:15:42,434 module.py:652] default: "GET / HTTP/1.1" 500 -
INFO 2014-11-12 18:28:21,151 module.py:652] default: "GET / HTTP/1.1" 200 1274
INFO 2014-11-12 18:28:21,272 module.py:652] default: "GET /styles/main.css HTTP/1.1" 200 1880
INFO 2014-11-12 18:28:27,512 module.py:652] default: "GET /downloads.html HTTP/1.1" 404 154
You have a handful of static directory routes defined in your app.yaml (images, scripts, styles) -- that's why your css file is loading fine, for example. Then you have everything not matching those directories going to "application" in your "main.py" file.
In there, you are currently only defining one route -- "/" and MainPage is defined as your handler for that route. Calls to "/downloads.html" get sent to that same router, but since it doesn't match the "/" route, a 404 is returned.
You need to define your other route(s) and handlers in your main.py. For example:
application = webapp2.WSGIApplication([
('/',MainPage),
('/downloads.html',DownloadPage)
], debug=True)
Then you'd define DownloadPage similarly to MainPage to handle that route. You can also do pattern matches in the route definition in order to pass along variable info, but for your example, the above should work.

GAE: 404 error on creating a new script

I'm using Google App Engine with Python environment.
I have my main code in the main.py file. I want to create a new .py file for a different page.
I created the .py file, added the path to the yaml file. But I still get a '404 Error, resource not found'.
Here is my yaml file
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: .*
script: main.app
- url: /hello
script: hello.app
libraries:
- name: webapp2
version: "2.5.2"
When the user goes to exampleurl.com/hello I want the hello.py file to be executed.
Here's the current content of hello.py
import webapp2
class HeyPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write('Hello, All!')
app = webapp2.WSGIApplication([('/hello', HeyPage)],
debug=True)
Here is the log:
INFO 2014-01-10 06:15:31,150 module.py:617] default: "GET /hello HTTP/1.1" 404 154
You should list your handlers from most specific to least specific. Your handler:
- url: .*
script: main.app
basically says that main.app should handle every url. Since it is the first in the list, main.py will try to handle every request regardless of the handlers that follow it in app.yaml. Change it to:
handlers:
- url: /hello
script: hello.app
- url: .*
script: main.app
And all should work.
As far as I can remember, GAE matches a URL with the patterns in handlers from top to down. .* matches with any URL and as it is the first pattern in the handlers section, it call main.app instead of your hello.app. You should place .* pattern at the end of the handlers section so that any URL that doesn't match with any of your previously defined URL patterns get handled by main.app.
So, modify your handlers section as:
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /hello
script: hello.app
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"

Google App Engine only deploying *some* images

I recently deployed a website for my dad using Google App Engine.
Oddly, some images in the gallery are found, while some return with a 404. I looked in the admin dashboard for usage rates, and it says I'm only at 17% of "Code and Static File Storage".
I have tried changing the directory and re-deploying, I have also created a second application (from this-site-1 to this-site-2), and I have waited about an hour in case it's simply the cache, but none of these seem to be the issue.
I'm brand new to Google App Engine, this is the first website I've deployed using it, so any help would be much appreciated.
app.yaml
application: this-site-2
version: 1
runtime: python
threadsafe: true
api_version: 1
handlers:
- url: /robots.txt
static_files: static/robots.txt
upload: static/robots.txt
- url: /css
static_dir: static/css
- url: /img
static_dir: static/img
- url: /js
static_dir: static/js
- url: /.*
script: main.py
Edit: I updated python to 2.7 in case that was the problem, but I did by doing this:
application: this-site-2
version: 1
runtime: python27
threadsafe: true
api_version: 1
handlers:
- url: /robots.txt
static_files: static/robots.txt
upload: static/robots.txt
- url: /css
static_dir: static/css
- url: /img
static_dir: static/img
- url: /js
static_dir: static/js
- url: /.*
script: main.app # a WSGI application in the main module's global scope
libraries:
- name: webapp2
version: "2.5.1"
- name: django
version: "1.2"
skip_files:
- ^(.*/)?app\.yaml
- ^(.*/)?app\.yml
- ^(.*/)?index\.yaml
- ^(.*/)?index\.yml
- ^(.*/)?#.*#
- ^(.*/)?.*~
- ^(.*/)?.*\.py[co]
- ^(.*/)?.*/RCS/.*
- ^(.*/)?\..*
- ^(.*/)?.*\.bak$
- ^(.*/)?.*\.less$
Then I deployed, and the console said that the deployment was successful and it was updating the indexes and exited with code 0. Then I tried to go to the website and it returned with a 500 server error. So I downgraded back to python 2.5 and the site is fine.
I think your image url handler is more complicated than it needs to be. I would suggest:
Put all static content (images, Javascript, CSS) in a folder called static or similar with subfolders for images, Javascript and CSS.
Use the following handler instead of your current image handler:
handlers:
- url: /images
static_dir: static/images
All files in the static/images folder will then be accessible on the url yourwebsite.com/images/subpath/image.jpg.
For more inspiration, see this app.yaml.
I had this problem and it turned out that it deploying images with lower case file extensions like this: name.png, but not ones with capital file extensions like this: name.PNG. Helpfully Windows hides this from you, so you can't always tell.
Ah, my dad solved it;
Simply change the jpg images that aren't working to a png image (and vice versa, if it's a png image not working, convert it to a jpg)

Categories