使用App Engine进行Remote_api配置

时间:2012-09-13 19:56:17

标签: python google-app-engine remoteapi

使用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所以我不会收到此错误?

谢谢!

2 个答案:

答案 0 :(得分:6)

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

您的app_id可能不对,您还需要传递地址参数。我一会儿又读了一下这本书,因为他读了一本关于发源的书(书已严重过时,不推荐)。

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)

答案 1 :(得分: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()