500 Server Error - ImportError: No module named ssl - python

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"

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.

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

No page is displayed for a very basic app in python+jinja2 on GAE

I am new to developing apps on google appengine and to python and jinja. I have been trying this since two days. My first app without jinja worked fine. However this particular app is not displaying anything on the browser. This is the main.py file
import webapp2
from webapp2_extra import jinja2
import logging
# this one is to help us parse an RSS feed
import feedparser
import urllib
class BaseHandler(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
class MainHandler(BaseHandler):
def get(self):
context = {}
self.render_response('index.html', **context)
# self.response.write('Byte1')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
And this is the app.yaml file
application: ykelkar-byte1
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
I tried to run the sample code that I had and it is getting displayed. So there shouldn't be any installation/configuration issue.
First thing I noticed is that your import is wrong:
from webapp2_extra import jinja2
should be:
# You have a missing s!
from webapp2_extras import jinja2
In general, during development you should enable debugging to catch simple errors like this by setting up your WSGIApplication as follows:
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
EDIT: Just noticed you already had debugging enabled. An error like this should have been showing you in your development console.
Are you using any particular IDE for development? I have found PyCharm to be a really good IDE for python development and google app engine specifically (The community edition is free). Maybe you should give it a try to help you out.

App Engine - Python - Open local HTML file from script

When testing a Google App Engine app with the Launcher, the Launcher will start a local server, and open a tab listening to http://localhost:8080/ If the App.yaml file is configured to point to a html file, then the webpage will open. Like if your home page is index.html
app.yaml file
application: myProjectName
version: 2
runtime: python27
threadsafe: false
api_version: 1
handlers:
- url: .*
static_dir: index.html
If the app.yaml is configured to point to a python script in the root url, I don't know how to get the script to load a web page:
app.yaml file
- url: /.*
script: main.py
If my main.py Python script is:
import webbrowser
webbrowser.open_new("README.html")
That will open the README.html file in my browser when I run the code from the Python Shell, but if I launch the app from Google App Engine Launcher, it won't load the html file. How do I get the .py file to open an HTML file after the Launcher has started the app on the localhost:8000 ?
I'm looking at a Google example, and it uses a WSGIApplication webapp I guess. First the Python code goes through an authorization process, then at the end of the script, there is this code:
# Create an WSGI application suitable for running on App Engine
application = webapp.WSGIApplication(
[('/', MainPage), ('/svc', ServiceHandler), ('/about', AboutHandler),
('/user', UserHandler)],
# XXX Set to False in production.
debug=True
)
def main():
"""Main entry point for executing a request with this handler."""
run_wsgi_app(application)
if __name__ == "__main__":
main()
I'd appreciate any feedback from anyone who has any experience with this.
The simplest example that I know is the hello world in the documentation:
# helloworld.py
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
The corresponding app.yaml is:
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.application
You create a class MainPage -- whenever you make a request to localhost:8080/ (note the slash is unncessary), you will be directed to the main page. the application is responsible for routing the request to the proper class and creating a new instance of that class for each request. It also calls the get or post or whatever HTTP method. Whatever you write into the response is what gets returned to the browser as the web page.
Now a single page isn't all that interesting. Maybe you want localhost:8080/goodbye as well. Then you just add another class and "register" it with the application:
# helloworld.py
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
class GoodbyePage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Goodbye, World. Time to sleep.')
application = webapp2.WSGIApplication([
('/', MainPage),
('/goodbye', GoodbyePage),
], debug=True)
No changes to app.yaml are necessary at this point.
Life might be kind of frustrating if you needed to keep all your pages in a single file though. We can break this into 2 (or more) files by modifying app.yaml.
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /goodbye
script: goodbye.app
- url: /.*
script: helloworld.application
helloworld.py is the same as the first example. goodbye.py looks like this:
# goodbye.py
import webapp2
class GoodbyePage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.write('<html><head></head><body>Goodbye!</body></html>')
app = webapp2.WSGIApplication([
('/goodbye', GoodbyePage),
], debug=True)
The urls in app.yaml are regular expressions -- you want to order them from most specific to least specific (otherwise, you might handle the request with the wrong handler). Also note that the convention in app.yaml is script: <module_name>.<variable_name> when we set it up this way.
Here is another example without Jinja2. To serve HTML with Python you simply need these 3 simple things.
1) Use the webapp2 Python module.
import webapp2
2) Read your HTML file into a variable:
INDEX_HTML = open('index.html').read()
3) Then write it out when a certain URL is sent:
self.response.out.write(INDEX_HTML)
This is the full Python script:
import cgi
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
INDEX_HTML = open('index.html').read()
self.response.out.write(INDEX_HTML)
class PageTwo(webapp2.RequestHandler):
def post(self):
self.response.write('<html><body>You wrote:<pre>')
self.response.write(cgi.escape(self.request.get('content')))
self.response.write('</pre></body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', PageTwo),
], debug=True)
Note that your app.yaml configuration needs to be correct, and you need an HTML file. The webapp2 module provides a RequestHandler that can route your URL requests. (Display different webpage content) Here is a GitHub repository with the code, manifest app.yaml file and the index.html file.
Serve HTML with Python
This gets the HTML from a file, then writes it to the browser. This is actually more of what I was looking for:
class MainPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render())
This is the entire module:
import cgi
import os
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class MainPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render())
class PageTwo(webapp2.RequestHandler):
def post(self):
self.response.write('<html><body>You wrote:<pre>')
self.response.write(cgi.escape(self.request.get('content')))
self.response.write('</pre></body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', PageTwo),
], debug=True)
Here is a link to the Git Hub repository:
Get Template HTML, and write to browser
I saw an interesting variation of the app.yaml configuration file in a code example:
- url: /
static_files: index.html
upload: index.html
Even though the webbapp2 Request Handler is serving a particular HTML file based on the URL, the app.yaml file also controls what happens when different URL's are sent. The app.yaml file can be configured to run the python script AND upload a particular HTML file. If your application has lots of sub-directories, and the main.py script doesn't load until the end of your app.yaml file, configuring the app.yaml file to load static HTML right up front may cause your main page to load faster:
application: YourProjectNameHere
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
#Even though the main.py script also loads the index,
#this is faster if lots of processing
- url: /
static_files: index.html
upload: index.html
- url: /Client_Style
static_dir: Client_Style
- url: /Client_Pages
static_dir: Client_Pages
- url: /Client_Script
static_dir: Client_Script
- url: /Library
static_dir: Library
- url: /apiclient
static_dir: apiclient
- url: /Client_Data
static_dir: Client_Data
- url: /oauth2client
static_dir: oauth2client
- url: /uritemplate
static_dir: uritemplate
- url: /.*
script: main.application
libraries:
- name: webapp2
version: latest

Python ImportError: No module named main in Google app engine project

I have the following app.yaml file
application: gtryapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /images/(.*\.(gif|png|jpg))
static_files: static/img/\1
upload: static/img/(.*\.(gif|png|jpg))
- url: /css/(.*\.css)
mime_type: text/css
static_files: static/css/\1
upload: static/css/(.*\.css)
- url: /js/(.*\.js)
mime_type: text/javascript
static_files: static/js/\1
upload: static/js/(.*\.js)
- url: /(.*\.html)
mime_type: text/html
static_files: static/\1
upload: static/(.*\.html)
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
And the file app.py:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
if self.request.url.endswith('/'):
path = '%sindex.html'%self.request.url
else:
path = '%s/index.html'%self.request.url
self.redirect(path)
application = webapp2.WSGIApplication([('/.*', MainPage)],
debug=True)
The files that I should deploy are just html files or js or images, I get the following error after compiling the app:
raise ImportError('%s has no attribute %s' % (handler, name))
ImportError: has no attribute app
Solved: I had to call "app" not "application" !
app = webapp2.WSGIApplication([('/.*', MainPage)],
debug=True)
You've called the file index.py, not main.py. Either rename it, or use index.app in the yaml.
The issue you are having is that your app.yaml file doesn't properly describe your code. Here is the offending bit:
- url: .*
script: main.app
This says that all URLs that were not matched by some previous entry should be handled by the app object of the main module, which should be a WSGI application object (see the WSGI standard).
This doesn't work because your code is set up differently. Your primary module is in index.py (the index module) and its interface with the server is via the CGI standard (though WSGI is used internally).
So, you need to change something. It could either be the app.yaml description of the app, or it could be the organization of your code.
Making your code work as a CGI-style program is easy. Just change app.yaml to point to index.py as the script. The .py part in this case is the file extension, and the file will be run as a script.
If instead you want to go with the newer, WSGI-compatible style (which is probably the best option), the documentation suggests the following format:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
app = webapp2.WSGIApplication([('/', MainPage)])
Your code is almost like this already. To make it work, get rid of your main function and if __name__ == "__main__" boilerplate. Replace it with:
app = webapp.WSGIApplication([('/.*', IndexHandler)],
debug=False)
This creates an app object at the top level of your module. Now, either rename your index.py file to main.py, or change app.yaml to point to index.app. The .app part of this is different this time. Rather than a file extension, it represents Python member access (in this case, accessing a global variable in a module).

Categories