Python 带应用程序引擎的远程api配置

Python 带应用程序引擎的远程api配置,python,google-app-engine,remoteapi,Python,Google App Engine,Remoteapi,使用Python,我试图连接到AppEngine应用程序的remote_api处理程序,但一直出现错误。我想做的是设置远程api存根,通过它路由数据库调用,并直接访问我的应用程序的数据存储。因此,首先,我导入远程api库,然后在存根模块上调用ConfigureRemoteApi,并尝试使用对远程数据存储的调用。下面是一些示例代码: from google.appengine.ext.remote_api import remote_api_stub def test_remote_api():

使用Python,我试图连接到AppEngine应用程序的remote_api处理程序,但一直出现错误。我想做的是设置远程api存根,通过它路由数据库调用,并直接访问我的应用程序的数据存储。因此,首先,我导入远程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访问应用程序会给我结果。我需要做什么来设置远程api存根,这样就不会出现此错误


谢谢

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

您的app_id可能错误,您还需要传递address参数。不久前,我在为appengine阅读oreilly的书时突然想到了这一点(这本书已经严重过时,不推荐使用)

def attach_to_app(app_id,user=None,password=None,path=None,address=None):
"""
附加到地址处的app_id实例(默认值:.appspot.com)
如果未指定用户/密码,系统将提示您输入
如果不指定地址,则假定为
.appspot.com
路径:远程api处理程序的路径(如果没有)
指定,将使用/\u ah/remote\u api
"""
路径=路径或“/\u ah/远程\u api”
如果没有地址:
address=“%s.appspot.com”%app\u id
初始化远程api(应用程序id、路径、地址、用户=用户、密码=密码)
def auth_func():
"""
函数提示用户输入凭据
"""
电子邮件地址=原始输入(“电子邮件地址:”)
password=getpass.getpass(“密码:”)
返回电子邮件地址、密码
def init_remote_api(应用程序id、路径、地址、用户=无、密码=无):
"""
用于初始化给定路径的remoteapi的通用包装器
必修的
=========
应用程序id:要连接到的应用程序的应用程序id
path:远程\u api处理程序的路径ex://\u ah/remote\u api
地址:要连接到的服务器例如:myapp.appspot.com
如果未指定用户和密码,系统将提示您
连接(如果需要)
"""
如果用户和密码:
def my_auth():
返回(用户、密码)
其他:
my_auth=auth_func
远程api存根。配置远程api(应用程序id、路径、我的身份验证、地址)
远程api存根.MaybeInvokeAuthentication()
操作系统环境[“服务器软件”]=“开发(远程api)/1.0”
#范例
APP_ID=“s~myapp”#引入hrds时,有必要指定APP_ID和地址,注意“s~…”
地址=“myapp.appspot.com”
将应用程序附加到应用程序(应用程序ID,地址=地址)

如果您只对访问数据存储感兴趣,可以通过以下方法避免存根错误:


你想实现什么?我在问题上说了这一点:“我想做的是设置远程api存根,通过它路由数据库调用,并直接访问我的应用程序的数据存储。”此外,“我需要做什么来设置远程api存根,这样我就不会出现此错误?”。如果这还不清楚,我希望能够从我的代码中使用远程api调用,而不是从远程api外壳调用我的应用程序代码。如果我的问题不清楚,我深表歉意。从代码内部使用远程api调用的目的是什么?我认为远程api的目的是能够从应用程序外部调用应用程序代码。这当然是可以做到的。我有一个项目,zope/plone可以通过远程api直接写入数据存储。ConfigureRemoteApi的第一个参数是appid,如下所示。好的,谢谢,Tom。现在,我收到了一个错误和一个我以前看到的警告:
BadRequestError:app s~myapp无法访问app dev~myapp的数据
并且
无法打开zipfile C:\Python27\lib\site packages\setuptools-0.6c11-py2.7.egg-info:IOError:[Errno 13]无法访问文件:“C:\\Python27\\lib\\site packages\\setuptools-0.6c11-py2.7.egg info”
。有什么想法吗?dev~。。。。指示您以某种方式指向本地开发服务器。你在为app_id和address传递什么?我是这么想的,但我一辈子都不明白为什么会发生这种情况。这是我的代码:
app\u id=“s~myapp”
address=“myapp.appspot.com”
path=“/\u ah/remote\u api”
。很抱歉格式化了。我在本地机器上运行这个,但我相信这不是错误的做法。毕竟,这个想法是从本地连接到服务器。有人告诉我,dev_appserver中当前形式的
remote_api_stub
将无法工作,因为它会丢弃原始的api stub。在SDK的
/ext/remote\u api/remote\u api\u stub.py
的第601行中,删除
apiproxy\u stub\u map.apipproxy=apipproxy\u stub\u map.apipproxy stubmap()
并用
replacetstub
替换这两个
RegisterStub
调用,然后将
服务=['datastore\u v3']
传递到
ConfigureRemoteApi
,它对我有效。正如在的评论,您应该更改默认分区以避免“无法访问应用”错误。谢谢。这似乎解决了问题,但遗憾的是,我正在开发的AppEngine应用程序已不再使用。因此,我无法测试这个+1,尽管如此。
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)
# 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()