GAE: 404 error on creating a new script - python

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"

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.

Constantly being unable to deploy app on app engine

Using google app engine launcher, I can't seem to deploy my app due to this:
email=martinchua99#gmail.com', '--passin', 'update', 'C:\Users\admin\Desktop\school work\customtinywebdb']"
Usage: appcfg.py [options] update | [file, ...]
appcfg.py: error: Error parsing C:\Users\admin\Desktop\school work\customtinywebdb\app.yaml: mapping values are not allowed here
in "C:\Users\admin\Desktop\school work\customtinywebdb\app.yaml", line 1, column 24.
2014-11-25 19:46:48 (Process exited with code 2)
This is my yaml file,whats wrong with it?
application: camel-cars: 1
runtime: python
api_version: 1
handlers:
- url: /images
static_dir: images
url: .*
script: main.py
Yaml files are sensitive to white spaces. Also you are missing key words and new lines. Give this a try:
application: camel-cars
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /images
static_dir: images
- url: .*
script: main.py

webapp2 routing can't route to handler - error 404

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

Using GAE Webapp2 with classes in more than one file

I'm having problems with Webapp2. When I put handlers for URLs that point to different python files in the app.yaml I get the following error:
ERROR 2012-10-06 16:44:57,759 wsgi.py:203]
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 195, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 250, in _LoadHandler
__import__(cumulative_path)
ImportError: No module named application
My app.yaml:
application: [[app's name]]
version: 1
runtime: python27
api_version: 1
threadsafe: yes
inbound_services:
- mail
handlers:
- url: /send
script: email.application
- url: /_ah/mail/update#sitdown-standup.appspotmail.com.*
script: email.application
- url: /.*
script: SDSUmodels.application
libraries:
- name: webapp2
version: "2.5.1"
SDSUmodels.py ends with:
application = webapp2.WSGIApplication([('/info', MakeBasicInfo)],
debug=True)`
and email.py ends with:
application = webapp2.WSGIApplication([('/request', Request_update),
('/send', Send_report),
(Receive_email.mapping())],
debug=True)`
When I remove these lines
- url: /send
script: email.application
from app.yaml, the error stops, but this leaves me without a way to point a URL towards a particular file.
I can see some alternative ways of handling this in this question but I was wondering why this approach isn't working. I've done this previously with the old webapp version in a different project and it's worked – details below.
app.yaml:
application: [[other app's name]]
version: 1
runtime: python
api_version: 1
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /twitter
script: twitter.py
- url: /_ah/mail/kindle#shelvdtracker.appspotmail.com.*
script: kindle.py
- url: /.*
script: web.py
inbound_services:
- mail
twitter.py ends with:
application = webapp.WSGIApplication(
[('/twitter', Process_new_DM)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
There is a standard library named email as well; it is being loaded before your local module is being found.
Rename the module to something else and it'll work.

Categories