用于序列化和反序列化旧值的库。

thriftrw的Python项目详细描述


buildcoveragedocs

thriftrw是用于序列化和反序列化精简类型的python库。

Documentation在上可用 阅读文档。

功能

  • 没有代码生成。.thrift文件在内存中进行分析和编译 在运行时。
  • 对字节的发送或接收方式没有任何假设。图书馆 只关心序列化和反序列化。
  • 支持Python2和3。
  • 类型中的正向和循环引用。

示例

给定.thrift文件,:

// blog.thrift

typedef string PostUUID

typedef binary RichText

union Body {
    1: string plainText
    2: RichText richText
}

struct PostDetails {
    1: required string author
    2: required string subject
    3: required Body body
}

exception UnauthorizedRequestError {
}

service BlogService {
    PostUUID newPost(1: PostDetails post)
        throws (1: UnauthorizedRequestError unauthorized);
}

您可以使用库发送和接收请求和响应,例如,

# client.pyimportthriftrwblog=thriftrw.load('blog.thrift')BlogService=blog.BlogServicedefnew_post():post=blog.PostDetails(author='...',subject='...',body=blog.Body(plainText='Hello, world!'))request=BlogService.newPost.request(post)payload=blog.dumps(request)# send_to_server is implemented by the user.response_payload=send_to_server(payload)response=blog.loads(BlogService.newPost.response,response_payload)ifresponse.unauthorizedisnotNone:raiseresponse.unauthorizedelse:returnresponse.success
# server.pyimportthriftrwblog=thriftrw.load('blog.thrift')BlogService=blog.BlogService# The user's server handler calls handle_new_post with the payload.defhandle_new_post(request_payload):request=blog.loads(BlogService.newPost.request,request_payload)ifrequest.post.author!='admin':response=BlogService.newPost.response(unauthorized=blog.UnauthorizedRequestError())else:# create_post is implemented by the user.post_uuid=create_post(request.post)response=BlogService.newPost.response(success=post_uuid)returnblog.dumps(response)

邮件信封

注意,这个示例只发送和接收请求/响应负载。它 不会像apache thrift所期望的那样将负载包装在消息信封中。 如果您想发送或接收标准的apache节俭请求以与其他 apache节俭服务,您必须使用loads.messagedumps.messageapi。例如,

# client.pydefnew_post():post=blog.PostDetails(...)request=BlogService.newPost.request(post)payload=blog.dumps.message(request)# ^ Instead of using blog.dumps, we use blog.dumps.message to indicate# that we want the request wrapped in a message envelope.response_payload=send_to_server(payload)# Similarly, instead of using blog.loads, we use blog.loads.message to# indicate that we want to parse a response stored inside a message.response_message=blog.loads.message(BlogService,response_payload)response=response_message.bodyifresponse.unauthorizedisnotNone:raiseresponse.unauthorizedelse:returnresponse.success
# server.pydefhandle_request(request_payload):message=blog.loads.message(BlogService,request_payload)ifmessage.name=='newPost':request=message.body# ...response=BlogService.newPost.response(success=post_uuid)returnblog.dumps.message(response,seqid=message.seqid)# As before, we use blog.dumps.message instead of blog.dumps.# It is important that the server use the same seqid in the# response as what was used in the request, otherwise the client# will not be able to process out-of-order responses.else:raiseException('Unknown method %s'%message.name)

有关详细信息,请参见Overview

注意事项

  • 此时只支持精简二进制协议。

许可证

Copyright (c) 2015 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在表被注释到配置之前获取表的元数据?   java滚动条不会出现在JList上   java JOGL监视器GPU内存   java为什么要使用RecyclerView onDraw延迟   java定制Oppo Reno 2 Z CPH1951(手机型号)的固件(闪存文件)   java自定义线程池执行器   java如何解决发布版本中重复的jar条目[com/安卓/volley/R.class]?   java如何使用Bukkit API触发事件?   java在blazemeter jmeter RTE插件中使用ctrl+w输入   C#/Visual Studio的java JDT等价物   java为什么当maxread值很大而收到的消息数量很小时,卡夫卡消费者会无限期消费?   java游戏2。x:包含模板列表的绑定模型   带压缩的java日志旋转   运行时。exec用java运行程序读取它正在做什么