在Python中使用HTTPS的Apache Thrift服务示例

2024-03-29 05:46:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经找到了examples如何使用Apache Thrift实现服务,后者使用SSL作为传输。。在爪哇。但不是用Python。

我想使用Apache Thrift生成用于调用Python编写的服务的样板代码,这些服务将从Android调用。传输必须是HTTPS。

有什么线索可以让我找到类似的东西吗?


Tags: 代码httpssslapache样板examplesthriftandroid
2条回答

我在PHP、Java和Python中使用过Thrift,您可能会注意到使用Thrift最糟糕的部分是它的文档。deofficer示例中的一部分,可以用不同的语言使用:Official Source Code Tutorial。下面是几个网页,它们更详细地描述了如何实现客户机/服务器节约协议:

通过SSL保护您的连接意味着通过添加几行新行来修改服务器/客户端,下面是Java中的一个示例:

将最后一段代码重写为python并不困难

你的客户会看起来像这样:

from thrift.transport import THttpClient
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from tutorial import Calculator

transport = THttpClient.THttpClient('https://your-service.com')
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Calculator.Client(protocol)

# Connect!
transport.open()
client.ping()

您可以在服务前面粘贴一个代理来终止SSL连接,然后将http请求传递给您的服务器,如下所示:

from thrift.protocol import TBinaryProtocol
from thrift.server import THttpServer

from tutorial import CalculatorHandler # assuming you defined this

handler = CalculatorHandler()
processor = Calculator.Processor(handler)
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = THttpServer.THttpServer(
    processor,
    ('', 9090),
    pfactory
)

print('Starting the server...')
server.serve()
print('done.')

相关问题 更多 >