go - golang and python thrift communcation -
i using thrift. here thrift file:
namespace py hello namespace go hello service hello{ string hellostring(1:string para) }
and generate python , golang code
thrift -gen py hello.thrift thrift -gen go hello.thrift
python server code: py-server.py
import socket import sys sys.path.append('./gen-py/') hello import hello hello.ttypes import * thrift.transport import tsocket thrift.transport import ttransport thrift.protocol import tbinaryprotocol thrift.server import tserver class hellohandler: def hellostring(self, msg): ret = "hellostring received: " + msg print ret return msg handler = hellohandler() processor = hello.processor(handler) transport = tsocket.tserversocket("127.0.0.1", 19090) tfactory = ttransport.tbufferedtransportfactory() pfactory = tbinaryprotocol.tbinaryprotocolfactory() server = tserver.tsimpleserver(processor, transport, tfactory, pfactory) print "starting thrift server in python..." server.serve()
and can run well:
$ python py-server.py starting thrift server in python...
and client writen golang, go-client.go:
package main import ( "./gen-go/hello" "fmt" "git.apache.org/thrift.git/lib/go/thrift" "net" "os" ) func main() { transportfactory := thrift.newtframedtransportfactory(thrift.newttransportfactory()) protocolfactory := thrift.newtbinaryprotocolfactorydefault() transport, err := thrift.newtsocket(net.joinhostport("127.0.0.1", "19090")) fmt.println(1) if err != nil { fmt.fprintln(os.stderr, "error resolving address:", err) os.exit(1) } usetransport := transportfactory.gettransport(transport) client := hello.newhelloclientfactory(usetransport, protocolfactory) fmt.println(2) if err := transport.open(); err != nil { fmt.println(3) fmt.fprintln(os.stderr, "localhsot:19090", " ", err) os.exit(1) } defer transport.close() fmt.println(4) r1, e1 := client.hellostring("lalalalalalalallalaal") fmt.println(5) fmt.println("call->", r1, e1) }
but can't run, output, , can not stop normally
$ go run go-client.go 1 2 4
and server not receive message.
is there problem in golang code?
if write client python , can run well..
import socket import sys sys.path.append('./gen-py/') hello import hello thrift import thrift thrift.transport import tsocket thrift.transport import ttransport thrift.protocol import tbinaryprotocol try: transport = tsocket.tsocket('127.0.0.1', 19090) transport = ttransport.tbufferedtransport(transport) protocol = tbinaryprotocol.tbinaryprotocol(transport) client = hello.client(protocol) transport.open() print "client - hellostring" msg = client.hellostring("lalalalalla") print "server - " + msg transport.close() except thrift.texception, ex: print "%s" % (ex.message) $ python py-client.py client - hellostring server - lalalalalla
transportfactory := thrift.newtframedtransportfactory(thrift.newttransportfactory())
please change newtframedtransportfactory
newtbufferedtransportfactory
, transportfactory := thrift.newtbufferedtransportfactory(1024)
, thanks.
Comments
Post a Comment