在中创建HttpClient节点.js三分之一

2024-09-23 22:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用thrift进行跨平台集成。我在thrift有一个python服务器。在

Python服务器

#!/usr/bin/env python

port = 30303
host = '127.0.0.1'
import sys
sys.path.append('gen-py')

from helloworld import HelloWorld
from helloworld.ttypes import *

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TJSONProtocol
from thrift.server import TServer
from thrift.server import THttpServer

import socket

class HelloWorldHandler:
  def __init__(self):
    self.log = {}

  def sayHello(self):
    print "sayHello()"
    return "say hello from " + socket.gethostbyname(socket.gethostname())    

handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket(host=host, port=port)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
#pfactory = TJSONProtocol.TJSONProtocolFactory()

#server = THttpServer.THttpServer(processor, (host, port), pfactory)
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print "Starting python server..."
server.serve()
print "done!"

我在thrift中创建了一个节点客户机,它能够在python中创建TSimpleServer时访问python服务器,但是在创建THttpServer时无法连接

节点客户端

^{pr2}$

我确保在python中运行THttpServer时使用了JSON协议。 我不知道如何在thrift中为node创建HttpClient。在

对不起,我把代码扔了,但我认为这会使问题更清楚。谢谢


Tags: fromimportself服务器hostserverportsys
1条回答
网友
1楼 · 发布于 2024-09-23 22:23:25

2014年4月23日添加了节点http客户端支持。目前对库的支持还只是在dev分支中,但在不久的将来,它将与0.9.2一起发布。下面是一个使用http节点连接/客户端的名为heloSvc的服务的客户端示例:

var thrift = require('thrift');
var helloSvc = require('./gen-nodejs/helloSvc.js');

var options = {
   transport: thrift.TBufferedTransport,
   protocol: thrift.TJSONProtocol,
   path: "/hello",
   headers: {"Connection": "close"}
};

var connection = thrift.createHttpConnection("localhost", 9090, options);
var client = thrift.createHttpClient(helloSvc, connection);

client.getMessage("Thurston Howell", function(error, result) {
  console.log("Msg from server: " + result);
});

相关问题 更多 >