2013-04-30 32 views
4

我已經能夠找到examples以瞭解如何使用Apache Thrift實現服務,該服務使用SSL作爲運輸..在Java中。但不是在Python中。在Python中使用HTTPS的Apache Thrift服務示例

我想使用Apache Thrift生成用於調用由Python編寫的將從Android調用的服務的樣板代碼。運輸需要是HTTPS。

任何線索,我可以找到類似的東西?

+1

我會認爲源將是一個很好的開始。乍一看,它似乎很好評論,並有一個類:'TSSLSocket' https://pypi.python.org/pypi/thrift/0.9.0 – 2013-04-30 12:52:16

+0

我希望有一些比閱讀源更友好的東西。 :) – wojciii 2013-04-30 13:00:39

+0

http://stackoverflow.com/questions/10964755/ssl-certificate-not-authenticating-via-thrift-but-ok-via-browser給我一些線索 - 所以我想讀源是我應該做的。你是正確的,來源是有據可查的。 :) – wojciii 2013-04-30 13:34:44

回答

1

我已經使用Thrift與PHP,Java和Python,並且您可能注意到使用Thrift的最糟糕的部分是它的文檔。來自de官方示例的一部分可用不同語言提供:Official Source Code Tutorial。這裏有幾個網頁的描述更詳細的如何實現客戶機/服務器節儉協議:

保護SSL上的連接將意味着修改你的服務器/客戶端加入了一些新的線路,這裏是Java中的例子:

它不是一個艱鉅的任務,以重寫了最後的代碼到Python

+0

AFAIK,建議不要在Python中終止您的SSL/TLS連接。在許多邊緣情況下,它會比像nginx這樣的代理做更差的工作。 – myyk 2016-02-02 00:21:59

1

你的客戶會是這個樣子:

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.') 
+0

這是我可以在網上找到的唯一的Python HTTP客戶機/服務器(也稱爲非套接字)示例。它的工作原理!謝謝。 – jfunk 2016-11-23 19:50:35

相關問題