Python. Error with remote_api, google app engine [closed] - python

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Following this guide:
http://code.google.com/appengine/articles/remote_api.html
I edited my app.yaml, updated the app, and tried to connect using api_remote. Then I got this error. Why?
c:\python25\python "C:\Program Files
\Google\google_appengine\remote_api_shell.py" -s medmcqs.appspot.com
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\remote_api_shell.py", line 99,
in <module>
run_file(__file__, globals())
File "C:\Program Files\Google\google_appengine\remote_api_shell.py", line 95,
in run_file
execfile(script_path, globals_)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\remote_a
pi_shell.py", line 140, in <module>
main(sys.argv)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\remote_a
pi_shell.py", line 136, in main
appengine_rpc.HttpRpcServer)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\remote_a
pi_shell.py", line 76, in remote_api_shell
rpc_server_factory=rpc_server_factory)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\remote_api
\remote_api_stub.py", line 645, in ConfigureRemoteApi
app_id = GetRemoteAppIdFromServer(server, path, rtok)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\remote_api
\remote_api_stub.py", line 501, in GetRemoteAppIdFromServer
'Invalid response recieved from server: %s' % response)
google.appengine.ext.remote_api.remote_api_stub.ConfigurationError: Invalid resp
onse recieved from server: <!DOCTYPE html PUBLIC "-//W3C//
This is what my app.yaml looks like...
application: medmcqs
version: 2
runtime: python
api_version: 1
inbound_services:
- mail
handlers:
- url: /static
static_dir: static
- url: /options
script: main.py
- url: /send
script: main.py
login: required
- url: /history
script: main.py
login: required
- url: /suggest
script: main.py
login: required
- url: .*
script: main.py
builtins:
- remote_api: on
UPDATE: Here are the log details:
2012-01-18 01:40:33.659 /_ah/remote_api?rtok=252529974978 200 48ms 1kb Google-remote_api/1.0 win32/6.1.7601.2 Python/2.5.4.final.0
109.224.29.101 - - [18/Jan/2012:01:40:33 -0800] "GET /_ah/remote_api?rtok=252529974978 HTTP/1.1" 200 1605 - "Google-remote_api/1.0 win32/6.1.7601.2 Python/2.5.4.final.0" "medmcqs.appspot.com" ms=49 cpu_ms=23 api_cpu_ms=0 cpm_usd=0.000855 instance=00c61b117cf891196f8b484d5951471ac52ac0

Some suggestions:
modify the .* handler
from :
-url: .*
script: main.py
To:
-url: /.*
script: main.py
Or: Maybe you can manually add the handler and try again to see if there are any differences.
- url: /_ah/remote_api
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
login: admin
EDIT1
There is another version of remote console code that you may want to try try. It is also from google app engine official website.
#remote_console.py
#!/usr/bin/python
import code
import getpass
import sys
sys.path.append(r"C:\Program Files\Google\google_appengine")
sys.path.append(r"C:\Program Files\Google\google_appengine\lib\yaml\lib")
sys.path.append(r"C:\Program Files\Google\google_appengine\lib\fancy_urllib")
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
def auth_func():
return raw_input('Username:'), getpass.getpass('Password:')
if len(sys.argv) < 2:
print "Usage: %s app_id [host]" % (sys.argv[0],)
app_id = sys.argv[1]
if len(sys.argv) > 2:
host = sys.argv[2]
else:
host = '%s.appspot.com' % app_id
remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
code.interact('App Engine interactive console for %s' % (app_id,), None, locals())
Usage:
remote_console.py YOUR_APP_ID YOUR_APP_ID.appspot.com
or
remote_console.py s~YOUR_APP_ID YOUR_APP_ID.appspot.com
For some reason, some app id will begin with s~.

Related

Sending emails from Google Appengine fails because of missing api proxy

I am trying to send e-mails from a GAE application using this code:
from google.appengine.api.mail import send_mail
send_mail(
"sender#nowhere.com",
["user#example.com"],
"Subject",
"Body",
)
I have configured usage of the apis in app.yaml with:
app_engine_apis: true
And deploy to App Engine is done with gcloud beta app deploy.
However, I get this error:
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/srv/infrastructure/view_modifiers.py", line 12, in view_method response_val = f(*args, **kwargs)
File "/srv/views/orders.py", line 25, in create_order vm.create_order()
File "/srv/viewmodels/orders/order_viewmodel.py", line 74, in create_order self._send_order_email()
File "/srv/viewmodels/orders/order_viewmodel.py", line 54, in _send_order_email send_mail(
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/mail.py", line 401, in send_mail message.send(make_sync_call=make_sync_call)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/mail.py", line 1209, in send make_sync_call('mail', self._API_CALL, message, response)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/apiproxy_stub_map.py", line 96, in MakeSyncCall return stubmap.MakeSyncCall(service, call, request, response)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/apiproxy_stub_map.py", line 348, in MakeSyncCall assert stub, 'No api proxy found for service "%s"' % service AssertionError: No api proxy found for service "mail"
This seems to suggest that even for the default behavior of the mail service, some kind of proxy needs to be configured. However, I cannot find any information about the setup of this proxy.
And, my initial understanding was that setting up a proxy is only needed for unit-testing or local development.
Here is an example that works with fastapi. (answer to anton's comment)
from google.appengine.api import mail
from fastapi.middleware.wsgi import WSGIMiddleware
from google.appengine.api import wrap_wsgi_app
from flask import Flask
app = create_app() # this is just app = FastAPI() and somes middleware but but no relevance here
app_flask = Flask(__name__)
app_flask.wsgi_app = wrap_wsgi_app(app_flask.wsgi_app, use_deferred=True)
def send_approved_mail(sender_address):
# [START send_message]
message = mail.EmailMessage(
sender=sender_address,
subject="Your account has been approved")
message.to = "test#gmail.com"
message.body = """Dear Albert:
Your example.com account has been approved. You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.
Please let us know if you have any questions.
The example.com Team
"""
message.send()
#app_flask.route("/send_email", methods=['GET'])
def send_email():
send_approved_mail("registred_email#domain.com")
return "message sent"
app.mount("/v1", WSGIMiddleware(app_flask))
here app.yaml
runtime: python39
entrypoint: gunicorn -k uvicorn.workers.UvicornWorker app.main:app
instance_class: F1
app_engine_apis: true
inbound_services:
- mail
- mail_bounce
You will not see flask route on swagger.
and for some reason, i could not sent email with app engine's default service account. I had to register my email ( with same domain) here : https://console.cloud.google.com/appengine/settings/emailsenders?project=your_project
I just tested this and it worked for me (i.e. I received the email I supplied for recipient_email_address in the code below). Note that sender must be a value specified under Email Senders in App Engine settings page
requirements.txt file
Flask
appengine-python-standard>=1.0.0
app.yaml file
runtime: python39
app_engine_apis: true
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
main.py
from flask import Flask
from google.appengine.api import wrap_wsgi_app
from google.appengine.api.mail import send_mail
app = Flask(__name__)
app.wsgi_app = wrap_wsgi_app(app.wsgi_app)
#app.route("/")
def sendMail():
send_mail(sender= <authorized_email_sender>,
to= <recipient_email_address>,
subject="Testing Python3 sending mails",
body="""Dear Albert:
Your example.com account has been approved. You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.
Please let us know if you have any questions.
The example.com Team
""")
return "Mail was sent"

python annoy library import error in GAE project

I am working on GAE using python jinja2 framework, while importing third party library "annoy" I am getting import error.
I am following the GAE Installing a third-party library method.
My project structure like
*projectfolder/
*lib/
*annoy/
main.py
app.yaml
appengine_config.py
Here is my main.py code
import os
import jinja2
from google.appengine.api import users
import webapp2
import csv
from jinja2 import Template
import ann_utils
JINJA_ENVIRONMENT = jinja2.Environment(
# TODO: to add other directories here that contains the templates.
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class Sent2Vec(webapp2.RequestHandler):
"""docstring for ."""
def get(self):
template = JINJA_ENVIRONMENT.get_template('sent2vec.html')
self.response.headers['Content-Type'] = 'text/html'
self.response.write(template.render()
ann_utils.py library
from annoy import AnnoyIndex
import random
f = 40
t = AnnoyIndex(f)
for i in xrange(1000):
v = [random.gauss(0, 1) for z in xrange(f)]
t.add_item(i, v)
t.build(10)
t.save('test.ann')
u = AnnoyIndex(f)
u.load('test.ann')
print(u.get_nns_by_item(0, 1000))
app.yaml
runtime: python27
threadsafe: ture
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
- name: annoy
version: latest
handlers:
- url: /.*
script: main.app
appengine_config.py
from google.appengine.ext import vendor
vendor.add('lib')
Here is the tracetrack:
INFO 2017-01-24 08:33:27,378 devappserver2.py:769] Skipping SDK update check.
INFO 2017-01-24 08:33:27,435 api_server.py:205] Starting API server at: http://localhost:60106
INFO 2017-01-24 08:33:27,451 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2017-01-24 08:33:27,451 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2017-01-24 08:34:53,286 wsgi.py:263]
Traceback (most recent call last):
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/home/affine/ML-Visualization/main.py", line 8, in <module>
from ann_utils import create_tree
File "/home/affine/ML-Visualization/ann_utils.py", line 1, in <module>
from annoy import AnnoyIndex
File "/home/affine/ML-Visualization/lib/annoy/__init__.py", line 15, in <module>
from annoylib import *
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 963, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named annoy.annoylib
INFO 2017-01-24 08:34:53,290 module.py:788] default: "GET / HTTP/1.1" 500 -

Google AppEngine with Service Account but invalid Credentials

I have tried to get a group settings with group settings API using Google App Engine 1.7.5 with python 2.5 following a simple example here.
this is my app.yaml:
application: {{APP_ID}}
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
and my main.py is like following:
def main():
application = webapp.WSGIApplication([('/', TestHandler)])
run_wsgi_app(application)
class TestHandler(webapp.RequestHandler):
"""Handles a request for the query page."""
def get(self):
self.response.out.write(Test())
def Test():
credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/apps.groups.settings')
http = credentials.authorize(httplib2.Http(memcache))
service = build('groupssettings', 'v1', http=http)
group = service.groups().get(groupUniqueId='{{GROUP_ID}}').execute()
logging.error(group)
if __name__=="__main__":
main()
and this is the stacktrace telling me invalid credentials!
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 714, in __call__
handler.get(*groups)
File "D:\Projets\GS\main.py", line 26, in get
self.response.out.write(Test())
File "D:\Projets\GS\main.py", line 41, in Test
group = service.groups().get(groupUniqueId='{{GROUP_ID}}').execute()
File "D:\Projets\GS\oauth2client\util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "D:\Projets\GS\apiclient\http.py", line 723, in execute
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 401 when requesting https://www.googleapis.com/groups/v1/groups/{{GROUP_ID}}?alt=json returned "Invalid Credentials">
INFO 2014-11-21 15:51:45,423 dev_appserver.py:3104] "GET / HTTP/1.1" 500 -
INFO 2014-11-21 15:51:45,611 dev_appserver.py:3104] "GET /favicon.ico HTTP/1.1" 404 -
Have anyone got this error before or have an idea of the root cause?
Have you turned on access to the groups API through the developer console for the project? This is a simple step that is often missed by people trying to get APIs going.
Also, I'd recommend using the decorator patterns for OAuth as described here to simplify the reasoning around authorization flows.
In addition, your app.yaml should specify python27 rather than just python. App Engine runs python27.

GAE Simple Request Handler only run once

Good day!
https://developers.google.com/appengine/docs/python/gettingstarted/helloworld
this is the hello world that I'm trying to run.
I can seeing the
Hello, world!
Status: 500
message. however it will be turned to a "HTTP Error 500" after I hit the refresh.
and... it seems that the appengine only shows me the good result once after I re-save either app.yaml or helloworld.py
This is the trace for the good result
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 187, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 239, in _LoadHandler
raise ImportError('%s has no attribute %s' % (handler, name))
ImportError: <module 'helloworld' from 'D:\work\[GAE] tests\helloworld\helloworld.pyc'> has no attribute app
INFO 2012-06-23 01:47:28,522 dev_appserver.py:2891] "GET /hello HTTP/1.1" 200 -
ERROR 2012-06-23 01:47:30,040 wsgi.py:189]
and this is the trace for the Error 500
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 187, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 239, in _LoadHandler
raise ImportError('%s has no attribute %s' % (handler, name))
ImportError: <module 'helloworld' from 'D:\work\[GAE] tests\helloworld\helloworld.pyc'> has no attribute app
INFO 2012-06-23 01:47:30,127 dev_appserver.py:2891] "GET /hello HTTP/1.1" 500 -
here's my helloworld.py
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
my main.py. (app is used instead of application)
import webapp2
class hello(webapp2.RequestHandler):
def get(self):
self.response.out.write('normal hello')
app = webapp2.WSGIApplication([
('/', hello),
], debug = True)
and the app.yaml
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /hello
script: helloworld.app
- url: /.*
script: main.app
libraries:
- name: webapp2
version: "2.5.1"
any clue what's causing this?
Regards,
You aren't creating a helloworld.app object in your helloworld.py module.
See the lines
app = webapp2.WSGIApplications([...
in your main.py file? That creates the main.app object that's referenced by the script: main.app handler in your app.yaml.
You're referencing a helloworld.app object a couple of lines above; that object doesn't exist. Python 2.7 in App Engine doesn't support the simple module model -- no WSGI handler, just a simple script -- that's used in the 2.5 "Hello World" demo.
As presveva said, use the 2.7 Getting Started guide for less confusion.

Workflow for configuring apache on a webfaction account via ssh and ftp. (django/python)

I'm new at this, however, when it comes to configuring mod_python/apache or wsgi/apache I suffer.
I've been able to use the python debugger tool.. pdb.set_trace() to success, especially when using the django development server, i.e. it out puts to the terminal all of the server activity, including the pdb interface.
So, how does one do something like this when trying to deploy a django website on an host such as webfaction?
Other than ftp into the error_log and read about it post failure, be able to interact with the system, as its happening?
Hopefully I'm clear enough here.
Btw, following is the file that I'm trying to configure.
import os
import sys
from os.path import abspath, dirname, join
from site import addsitedir
from django.core.handlers.modpython import ModPythonHandler
import pdb
class PinaxModPythonHandler(ModPythonHandler):
def __call__(self, req):
# mod_python fakes the environ, and thus doesn't process SetEnv.
# This fixes that. Django will call this again since there is no way
# of overriding __call__ to just process the request.
os.environ.update(req.subprocess_env)
from django.conf import settings
sys.path.insert(0, abspath(join(dirname(__file__), "../../")))
sys.path.insert(0, os.path.join(settings.PINAX_ROOT, "apps/external_apps"))
sys.path.insert(0, os.path.join(settings.PINAX_ROOT, "apps/local_apps"))
sys.path.insert(0, join(settings.PINAX_ROOT, "apps"))
sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
pdb.set_trace()
return super(PinaxModPythonHandler, self).__call__(req)
def handler(req):
# mod_python hooks into this function.
return PinaxModPythonHandler()(req)
and here's the resulting error page via http:
MOD_PYTHON ERROR
ProcessId: 318
Interpreter: 'web25.webfaction.com'
ServerName: 'web25.webfaction.com'
DocumentRoot: '/etc/httpd/htdocs'
URI: '/'
Location: '/'
Directory: None
Filename: '/etc/httpd/htdocs'
PathInfo: '/'
Phase: 'PythonHandler'
Handler: 'bc.deploy.modpython'
Traceback (most recent call last):
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)
File "/home/dalidada/webapps/birthconfidence/bc/deploy/modpython.py", line 33, in handler
return PinaxModPythonHandler()(req)
File "/home/dalidada/webapps/birthconfidence/bc/deploy/modpython.py", line 29, in __call__
return super(PinaxModPythonHandler, self).__call__(req)
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/django/core/handlers/modpython.py", line 191, in __call__
self.load_middleware()
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/django/core/handlers/base.py", line 40, in load_middleware
raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
ImproperlyConfigured: Error importing middleware django_openid.consumer: "No module named django_openid.consumer"
How to use pdb with mod_wsgi is documented on the mod_wsgi site. See:
http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Python_Interactive_Debugger
Other debugging techniques are shown on the same page.

Categories