【问题标题】:Remote_api configuration with App Engine使用 App Engine 配置 Remote_api
【发布时间】:2012-09-06 23:42:00
【问题描述】:

使用 Python,我正在尝试连接到我的 AppEngine 应用程序的 remote_api 处理程序,但我不断收到错误消息。我想要做的是设置 remote_api 存根以通过它路由数据库调用并直接访问我的应用程序的数据存储。因此,首先,我导入 remote_api 库,然后在存根模块上调用 ConfigureRemoteApi,并尝试使用对远程数据存储的调用。下面是一些示例代码:

from google.appengine.ext.remote_api import remote_api_stub

def test_remote_api():
    # This function is called when I want to use the remote api instead of the local datastore access
    remote_api_stub.ConfigureRemoteApi('myapp.appspot.com', '/_ah/remote_api', auth_func, '')

def auth_func:
    # This actually returns a tuple with my credentials to skip the console input
    return ('username', 'password')

好的,现在,我已经使用 remote_api_shell.py 测试了我的登录信息和应用名称,但我收到如下错误:

File "C:\Program Files(x86)\Google\google_appengine\google\appengine\tools\dev_appserver_blobstore.py", line 79, in GetBlobStorage
return apiproxy_stub_map.apiproxy.GetStub('blobstore').storage
AttributeError: 'RemoteStub' object has no attribute 'storage'

然后我得到一个 404: Not Found 从应用程序,我知道这是错误的,因为从 Web 访问应用程序确实给了我结果。我需要做些什么来设置 remote_api_stub 以免出现此错误?

谢谢!

【问题讨论】:

  • 你想达到什么目的?
  • 我在我的问题中说明了这一点:“我想要做的是设置 remote_api 存根以通过它路由数据库调用并直接访问我的应用程序的数据存储区。”另外,“我需要做什么来设置 remote_api_stub,这样我就不会收到这个错误?”。如果不清楚,我希望能够从 inside 我的代码使用 remote_api 调用,而不是从 remote_api shell 调用我的应用程序代码。
  • 如果我的问题不清楚,我深表歉意。从代码内部使用 remote_api 调用的目的是什么?我认为远程 api 的目的是能够从您的应用程序外部调用您的应用程序代码。
  • 这当然是可行的。我有一个项目,其中 zope/plone 可以通过 remote_api 直接写入数据存储。 ConfigureRemoteApi 的第一个参数是 appid,根据下面的答案。

标签: python google-app-engine remoteapi


【解决方案1】:

如果您只对访问数据存储感兴趣,则可以使用 testBed 避免存根错误:

# First, create an instance of the Testbed class.
myTestBed = testbed.Testbed()

# Then activate the testbed, which prepares the service stubs for use.
myTestBed.activate()

# Next, declare which service stubs you want to use.
myTestBed.init_blobstore_stub()
myTestBed.init_logservice_stub()

【讨论】:

  • 谢谢。这似乎解决了这个问题,虽然遗憾的是我正在开发的 AppEngine 应用程序不再使用。因此,我无法对此进行测试。 +1,不过。
【解决方案2】:

假设您现在正在使用高复制数据存储...

你的app_id可能是错误的,还需要传递地址参数。不久前,我在阅读 appengine 的 oreilly 书时突然想到了这一点(书严重过时,不推荐)

def attach_to_app(app_id, user=None, password=None, path=None, address=None):
    """
    attaches to app_id instance at address (default: <app_id>.appspot.com)

    if you don't specify a user/password you will be prompted for one

    if you don't specify an address it is assumed to be
    <app_id>.appspot.com

    path: path to remote_api handler, if not
    specified, will use /_ah/remote_api 
    """
    path = path or "/_ah/remote_api"

    if not address:
        address = "%s.appspot.com" % app_id

    init_remote_api(app_id, path, address, user=user, password=password)




def auth_func():
    """
    function to prompt user for credentials
    """
    email_address = raw_input("Email Address: ")
    password = getpass.getpass("Password: ")
    return email_address, password


def init_remote_api(app_id, path, address, user=None, password=None):
    """
    generic wrapper to initialize the remoteapi for a given path

    required
    =========
    app_id : the app_id of the application to connect to
    path: the path to the remote_api handler ex: /_ah/remote_api
    address: server to connect to ex: myapp.appspot.com

    if user and password are not specified you will be prompted on
    connection if needed
    """
    if user and password:
        def my_auth():
            return (user, password)
    else:
        my_auth = auth_func

    remote_api_stub.ConfigureRemoteApi(app_id, path, my_auth, address)
    remote_api_stub.MaybeInvokeAuthentication()
    os.environ["SERVER_SOFTWARE"] = "Development (remote_api)/1.0"

# example
APP_ID = "s~myapp" # when hrds was introduced it became necessary to specify app_id and address note the "s~..."
ADDRESS = "myapp.appspot.com"
attach_to_app(APP_ID, address=ADDRESS)

【讨论】:

  • 好的,谢谢,汤姆。现在,我收到了一个以前见过的错误和警告:BadRequestError: app s~myapp cannot access app dev~myapp's dataCan't open zipfile C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info: IOError: [Errno 13] file not accessible: 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info'。对此有何想法?
  • dev~.... 表明您以某种方式指向本地开发服务器。你为 app_id 和地址传递了什么?
  • 我就是这么想的,但我终生无法弄清楚为什么会这样。这是我的代码:app_id = "s~myapp"address = "myapp.appspot.com"path = "/_ah/remote_api"。抱歉格式化。我在本地机器上运行它,但我相信这不是错误的做法。毕竟,这个想法是从本地连接到服务器。
  • 有人告诉我 remote_api_stub 在 dev_appserver 中的当前形式将无法工作,因为它会丢弃原始 API 存根。在 SDK 的 /ext/remote_api/remote_api_stub.py 的 ~601 行中,删除 apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() 并用 ReplaceStub 替换两个 RegisterStub 调用,然后将 services=['datastore_v3'] 传递给 ConfigureRemoteApi,它对我有用。
  • 正如link 评论的那样,您应该更改默认分区以避免“无法访问应用程序”错误。
猜你喜欢
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 2015-12-08
  • 2011-05-06
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多