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

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.

Related

500 Server Error - ImportError: No module named ssl

I've been using Google App Engine, and I've been having a lot of trouble with deploying my app. I've connected my source repository from Github on Google Cloud Platform, and I'm using Windows and Python 3.7 for this, for reference.
I've tried looking for similar issues to mine and trying out different solutions, as well as trying the GAE documentation to deploy my app, but I still end up getting "500 Server Error" instead and my page tells me "Error: Server Error". When I check Error Reporting, the error says "ImportError: No module named ssl", but I don't know how to deal with that.
As such, I wanted to know what could be causing this, be it still something in my code, or could it be related to my directory? Any response would be much appreciated.
The following is my "main.py" code:
import webapp2
import jinja2
import os
from webapp2_extras import sessions
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
#webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class MainPage(BaseHandler):
def get(self):
main_template = jinja_env.get_template('templates/index.html')
self.response.write('Hello, World!')
name = self.request.get("name") # name
self.session['name'] = name
self.response.write(main_template.render(name=name))
def post(self):
main_template = jinja_env.get_template('templates/index.html')
self.response.write('Inputted text: ' % self.request.get('name'))
name = self.request.get("name") # Category
self.session['name'] = name
self.response.write(main_template.render(name=name))
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key',
}
app = webapp2.WSGIApplication([
('/', MainPage)
], config=config,
debug=True)
And this is my "app.yaml" code:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /static
static_dir: static
- url: /.*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
- name: ssl
version: latest
Try putting brackets around latest in your definition for app.yaml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /static
static_dir: static
- url: /.*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: "latest"
- name: ssl
version: "latest"

googleappengine SDK home page and app url issue

url = http://localhost:8000/instances
app.yaml
application: 222999888
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: webapp2
version: "2.5.2"
- name: webob
version: "1.2.3"
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /hello
script: main.app
main.py
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
Now I dont know how to access hello world text through http://localhost:8000/hello url because when I try to visit this url it says 404.
where am I making mistake
I'm following this page https://developers.google.com/appengine/training/intro/gettingstarted
PS:
using ubuntu+python2.7.6+commandline interface
EDIT:
WARNING 2014-09-01 06:07:35,845 api_server.py:383] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO 2014-09-01 06:07:35,848 api_server.py:171] Starting API server at: http://localhost:58535
INFO 2014-09-01 06:07:35,850 dispatcher.py:183] Starting module "default" running at: http://localhost:8080
INFO 2014-09-01 06:07:35,852 admin_server.py:117] Starting admin server at: http://localhost:8000
INFO 2014-09-01 06:08:10,090 module.py:652] default: "GET / HTTP/1.1" 404 -
The web server listens on port 8080 by default. You can visit the application at this URL: http://localhost:8080/.
What you were looking at is The Development Console.
Here's everything you need to know about The Python Development Server.

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

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"

app engine: incoming emails get rejected

I'm trying to get a barebone app engine app to handle incoming email. I've followed the Receiving Email tutorial, and my code is really minimal.
However, when I send an email to say whatever#myapplication.appspot.com, Google rejects it before it gets to my app:
Delivery to the following recipient failed permanently:
whatever#myapplication.appspot.com
Technical details of permanent failure: Google tried to deliver your
message, but it was rejected by the recipient domain.
this is my app.yaml
application: myapplication
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /_ah/mail/.+
script: handle_incoming_email.py
- url: /.*
script: myapplication.app
inbound_services:
- mail
and handle_incoming_email.py is taken from the tutorial:
import logging, email
from google.appengine.ext import webapp
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp.util import run_wsgi_app
class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
logging.info("Received a message from: " + mail_message.sender)
Any idea why emails are being rejected?
It seems that you missed login: admin in the yaml file.
and it's whatever#myapplication.appspotmail.com not whatever#myapplication.appspot.com.

Categories