Python 哥朗与蟒蛇节俭交流
我在节俭。 这是旧档案:Python 哥朗与蟒蛇节俭交流,python,go,thrift,Python,Go,Thrift,我在节俭。 这是旧档案: namespace py hello namespace go hello service Hello{ string helloString(1:string para) } 然后我生成python和golang代码 thrift -gen py Hello.thrift thrift -gen go Hello.thrift Python服务器代码:py-server.py import socket import sys sys.path.a
namespace py hello
namespace go hello
service Hello{
string helloString(1:string para)
}
然后我生成python和golang代码
thrift -gen py Hello.thrift
thrift -gen go Hello.thrift
Python服务器代码:py-server.py
import socket
import sys
sys.path.append('./gen-py/')
from hello import Hello
from hello.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from 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()
我能跑得很好:
$ python py-server.py
Starting thrift server in python...
我的客户是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)
}
但它不能运行,不能输出,不能正常停止
$ go run go-client.go
1
2
4
服务器没有收到任何消息
我的golang代码有问题吗
如果我用python编写客户机,它可以运行良好
import socket
import sys
sys.path.append('./gen-py/')
from hello import Hello
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from 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.newFramedTransportFactory(thrift.newTransportFactory())
请将
newFramedTransportFactory
更改为newBufferedTransportFactory
,就像transportFactory:=thrift.newBufferedTransportFactory(1024)
,谢谢。thrift版本0.9.2 go版本go1.4.2 darwin/amd64 Python 2.7.5