如何在Django视图中捕获和修改Google协议缓冲区? [英] How to capture and modify Google Protocol Buffers in a Django view?

查看:124
本文介绍了如何在Django视图中捕获和修改Google协议缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是原文件

请有人帮我了解如何使这项工作:

Please can someone help me understand how to make this work:

from django.views.decorators.csrf import csrf_exempt
from bitchikun import payments_pb2

@csrf_exempt
def protoresponse(request):
    xpo = payments_pb2.Payment.ParseFromString(request)
    t = type(xpo)

    xpa = request.PaymentACK
    xpa.payment = xpo.SerializeToString()
    xpa.memo = u'success'
    return HttpResponse(xpa.SerializeToString(), content_type="application/octet-stream")

所有输入赞赏:)

推荐答案

OK,所以我想我明白发生了什么现在。您有一个系统,它将一个序列化的protobuf发送到您的Django应用程序,您需要返回另一个protobuf作为响应。

OK so I think I understand what is happening now. You have a system which is POSTing a serialized protobuf to your Django app, and you need to return another protobuf in response.

在Django中,您可以访问POST在 request.body 中。这可能是你需要传递给 ParseFromString

In Django, you can access the data from a POST in request.body. That is probably what you need to pass to ParseFromString.

你还有一些其他错误:你引用 request.PaymentACK ,它不存在 - 你的意思是 payments_pb2.PaymentACK - 你从来没有实际实例化。此外,您正在尝试将原始请求protobuf的序列化版本传递给该响应,当您应该传递实际的消息时。

You have some other errors too: you refer to request.PaymentACK, which doesn't exist - you mean payments_pb2.PaymentACK - and you never actually instantiate it. Also, you are trying to pass the serialized version of the original request protobuf to that response one, when you should be passing the actual message.

所以,它总是如下所示:

So, altogether it would look like this:

xpo = payments_pb2.Payment.FromString(request.body)
xpa = payments_pb2.PaymentACK()
xpa.payment = xpo
xpa.memo = u'success'
return HttpResponse(xpa.SerializeToString(), content_type="application/octet-stream")

这篇关于如何在Django视图中捕获和修改Google协议缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆