5

I have been able to find examples for how to implement services using Apache Thrift which use SSL as transport .. in Java. But not in Python.

I would like to use Apache Thrift to generate the boilerplate code for calling services written in Python which will be called from Android. The transport needs to be HTTPS.

Any clues where I can find something like that?

wojciii
  • 4,253
  • 1
  • 30
  • 39
  • 1
    I would think the source would be a good place to start. At a quick glance it seems well commented and there is a class: `TSSLSocket` https://pypi.python.org/pypi/thrift/0.9.0 – Chris J Apr 30 '13 at 12:52
  • I was hoping for something more friendly than reading the source. :) – wojciii Apr 30 '13 at 13:00
  • http://stackoverflow.com/questions/10964755/ssl-certificate-not-authenticating-via-thrift-but-ok-via-browser gives me some clues - so I guess reading the source is what I should do. You are correct that the source is well documented. :) – wojciii Apr 30 '13 at 13:34

2 Answers2

3

Your client would look something like this:

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()

You can stick a proxy in front of your service to terminate the SSL connection and then pass on the http request to your server that looks something like this:

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.')
myyk
  • 1,537
  • 1
  • 15
  • 35
  • This is the only Python HTTP client/server (aka not sockets) example I could find online. And it works! Thanks. – jfunk Nov 23 '16 at 19:50
2

I have used Thrift with PHP, Java and Python, and you may noticed the worst part of working with Thrift is its documentation. A part from de official example that is available in different languages: Official Source Code Tutorial. Here are a couple of webpages that describes more detailed how to implement a client/server thrift protocol:

Securing your connection over SSL will imply to modify your server/client by adding a couple of new lines, here is an example in Java:

It is not a tough task to rewrite the last code to python

jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
  • AFAIK, it's not recommended to terminate your SSL/TLS connections in Python. It will do a poorer job than a proxy like nginx in many edge cases. – myyk Feb 02 '16 at 00:21