0

2 つの GAE アプリが連携して動作しています。1 つはデータベースにオブジェクトを保持し、もう 1 つは最初のアプリからそのオブジェクトを取得します。以下に、最初のアプリが要求され、Critter オブジェクトを提供するコードを少し示します。urllib2 経由で最初のアプリのオブジェクトにアクセスしようとしていますが、これは本当に可能ですか? json に使用できることはわかっていますが、オブジェクトに使用できますか?

いくつかのコンテキストのために、これをクラスのプロジェクトとして開発しています。学生は、クリッターを作成することで、GAE アプリをホストする方法を学びます。次に、彼らはクリッターの URL を私に提供し、私のアプリはその URL を使用してすべてのクリッターを収集し、それらを私のアプリの世界に配置します。

私はピクルスについて最近聞いたばかりで、まだ調べていません。それはより良い代替手段でしょうか?

critter.py:

class Access(webapp2.RequestHandler):
    def get(self):
        creature = CritStore.all().order('-date').get()
        if creature:
            stats = loads(creature.stats)
            return SampleCritter(stats)
        else:
            return SampleCritter() 

map.py:

class Out(webapp2.RequestHandler):
    def post(self):
        url = self.request.POST['url']#from a simple html textbox
        critter = urllib2.urlopen(url)
        ...work with critter as if it were the critter object...
4

1 に答える 1

0

はい、ピクルスを使用できます。

キーを含むエンティティを転送するサンプル コードを次に示します。

entity_dict = entity.to_dict() # First create a dict of the NDB entity
entity_dict['entity_ndb_key_safe'] = entity.key.urlsafe() # add the key of the entity to the dict
pickled_data = pickle.dumps(entity_dict, 1) # serialize the object
encoded_data = base64.b64encode(pickled_data) # encode it for safe transfer

urllib2 の代替として、GAE urlfetch.fetch() を使用できます。

リクエストしているアプリでは、次のことができます。

entity_dict = pickle.loads(base64.b64decode(encoded_data)) 
于 2013-06-26T21:29:38.147 に答える