thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Polvi <a...@polvi.net>
Subject Re: Is anybody use twisted(Python) & thrift?
Date Wed, 17 Jun 2009 20:15:04 GMT
On Wed, Jun 17, 2009 at 11:46 AM, Eric Evans<eevans@sym-link.com> wrote:
> [ Esteve Fernandez ]
>> Are you wrapping your transport with TFramedTransport? In any case, could you
>> post both your client and your server?
>
> Hi Esteve,
>
> I'm having some trouble getting a Twisted client working with a Java
> server, and I noticed that you said something similar in THRIFT-148.
>
> Can you provide an example of Twisted client code that uses
> TFramedTransport?

Not sure if this will help, but here is a very bare bones twisted
thrift client, server, and example thrift file.

###### Twisted Thrift Client
#!/usr/bin/python

from twisted.internet import reactor, defer
from twisted.internet.protocol import ClientCreator
from thrift.transport import TTwisted
from thrift.protocol import TBinaryProtocol
from example import ExampleService

@defer.inlineCallbacks
def got_proto(proto):
  # now actually make a thrift call and print the results
  res = yield proto.client.do_something('foo')
  print res
  reactor.stop()

d = ClientCreator(reactor,
                  TTwisted.ThriftClientProtocol,
                  client_class=ExampleService.Client,
                  iprot_factory=TBinaryProtocol.TBinaryProtocolFactory(),
                 ).connectTCP("localhost", 9999)

d.addCallback(got_proto)

reactor.run()

##### Twisted Thrift Server
#!/usr/bin/python

from twisted.internet import reactor
from thrift.transport import TTwisted
from thrift.protocol import TBinaryProtocol

from example import ExampleService

import zope

class ExampleHandler(object):
  zope.interface.implements(ExampleService.Iface)

  def do_something(self, fun_string):
    return fun_string*5

reactor.listenTCP(9999, TTwisted.ThriftServerFactory(
                          processor=ExampleService.Processor(ExampleHandler()),

iprot_factory=TBinaryProtocol.TBinaryProtocolFactory()))

reactor.run()

#### Example.thrift for this code
#!/usr/local/bin/thrift --gen py:twisted

service ExampleService {
  string do_something(1: string fun_string)
}

Hope this helps, happy to explain anything as needed,

-Alex
-- 
twitter.com/polvi

Mime
View raw message