1

我正在使用 GAE SDK 1.8

我正在尝试对以下内容进行 jsonify:

{
    'prev': None, 
    'records': 1, 
    'next': None, 
    'start': 'http://127.0.0.1:9080/v1/requests/ag1kZXZ-Y3VybGF0cm9uchQLEgdSZXF1ZXN0GICAgICAkIAIDA/transactions?page=1&per_page=50', 
    'items': [
              {
               'customer': {'href': 'ag1kZXZ-Y3VybGF0cm9uchULEghDdXN0b21lchiAgICAgMDvCAw'}, 
               'response_headers': {
                   'x-powered-by': 'PHP/5.3.8', 
                   'transfer-encoding': 'chunked', 
                   'vary': 'Accept-Encoding', 
                   'server': 'Apache/2.2.20 (Unix)', 
                   'date': 'Thu, 09 May 2013 12:14:44 GMT', 
                   'content-type': 'text/html; charset=UTF-8'
               }, 
               'success': True, 
               'url': u'https://recurly-util.pagodabox.com/recurly-notification',
               'response_status': 200, 
               'created_at': '2013-05-09T12:14:44.446901', 
               'request': {'href': 'ag1kZXZ-Y3VybGF0cm9uchQLEgdSZXF1ZXN0GICAgICAkIAIDA'},
               'response_payload': u'html response removed for brevity',
               'request_payload': u'{"created_at": "2013-05-09 12:14:39.393591", "event": {"data": {"test": "test"}, "name": "Model Save"}}', 
               'method': u'post', 
               'href': 'http://127.0.0.1:9080/v1/transactions/ag1kZXZ-Y3VybGF0cm9uch8LEhJSZXF1ZXN0VHJhbnNhY3Rpb24YgICAgICQoAkM', 
               'trigger': {'href': 'ag1kZXZ-Y3VybGF0cm9uchQLEgdUcmlnZ2VyGICAgICA4L8IDA'}, 
               'request_headers': None, 
               'event': {'href': 'ag1kZXZ-Y3VybGF0cm9uchILEgVFdmVudBiAgICAgMCfCAw'}, 'handler': None}
            ]
}

我得到了闲散的回溯

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 224, in Handle
    for chunk in result:
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/profiler.py", line 542, in __call__
    for value in result:
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/profiler.py", line 418, in profile_start_response
    yield result_fxn_wrapper(result.next)
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/instrumented_profiler.py", line 70, in run
    return self.c_profile.runcall(lambda *args, **kwargs: fxn(), None, None)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py", line 149, in runcall
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/instrumented_profiler.py", line 70, in <lambda>
    return self.c_profile.runcall(lambda *args, **kwargs: fxn(), None, None)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/appstats/recording.py", line 1289, in appstats_wsgi_wrapper
    for value in result:
  File "/Users/mbeale/python/curlatron/werkzeug/debug/__init__.py", line 98, in debug_application
    ignore_system_exceptions=True)
  File "/Users/mbeale/python/curlatron/werkzeug/debug/tbtools.py", line 155, in get_current_traceback
    tb = Traceback(exc_type, exc_value, tb)
  File "/Users/mbeale/python/curlatron/werkzeug/debug/tbtools.py", line 206, in __init__
    self.frames.append(Frame(exc_type, exc_value, tb))
  File "/Users/mbeale/python/curlatron/werkzeug/debug/tbtools.py", line 347, in __init__
    fn = inspect.getsourcefile(tb) or inspect.getfile(tb)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 456, in getsourcefile
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 506, in getmodule
KeyError: '__main__'

我已将问题缩小到 response_headers 字段。当我删除一切正常。我还觉得奇怪的是,如果我用完全相同的字段替换该字段,它可以正常工作而不会出错。

collection['items'][0]['response_headers'] = {'x-powered-by': 'PHP/5.3.8', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'Apache/2.2.20 (Unix)', 'date': 'Thu, 09 May 2013 12:15:01 GMT', 'content-type': 'text/html; charset=UTF-8'

我正在使用 ndb.PickleProperty 将值存储在数据存储中。我正在从以下位置检索信息:

    resp = urlfetch.fetch(rt.url, payload=rt.request_payload, method=rt.method, headers=new_headers, allow_truncated=False, follow_redirects=False, deadline=30)
    if resp.status_code < 299 and resp.status_code > 199:
        rt.success = True
        req.state = "completed"
        req.put()
        logging.info('Success:%s' % resp.status_code)
    rt.response_status = resp.status_code
    rt.response_headers = resp.headers

为什么我会收到此错误?在保存到数据存储之前,我应该对返回的响应标头进行一些编码吗?如果您需要更多信息,请告诉我。

4

1 回答 1

1

我的问题是 ndb.PickleProperty 默认没有给我有效的 JSON。我必须执行 dict(model.response_header) 才能获得有效的 JSON。

于 2013-05-10T22:32:39.043 回答