从Thrift javascript客户端(CORS)调用Thrift python服务器(使用浏览器扩展修复)

2024-09-23 22:32:33 发布

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

我正试图从一个javacript-thrift客户机连接到python-thrift服务器。目前我只是使用一个教程代码,我将在稍后的建设。python服务器代码可以在这里找到->;Python server code

javscript代码如下

    thriftCall:function() {

    var transport = new Thrift.Transport("http://localhost:30303/pythonImpl/pythonServer");
    var protocol  = new Thrift.Protocol(transport);
    var client    = new HelloWorldClient(protocol);



    try {
        result = client.ping();
        cc.log(result);

        result = client.sayHello();
        cc.log(result);

        result = client.sayMsg(HELLO_IN_KOREAN);
        cc.log(result);
    }catch(ouch){
        cc.log("Problem!!!");
    }
}

当我尝试在服务器上连接以下消息时出错

^{pr2}$

我不知道如何处理这个问题。从python客户机调用同一个服务器可以正常工作。任何帮助或线索都将非常感谢。在

更新: 我不得不对python使用JSON协议

handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket(port=30303)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TJSONProtocol.TJSONProtocolFactory() #pfactory = TBinaryProtocol.TBinaryProtocolFactory()
port = 30303
#server = THttpServer.THttpServer(processor, transport, tfactory, pfactory)
server = THttpServer.THttpServer(processor, ("localhost", port), pfactory)

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

所以现在js客户端可以连接了,但是我得到了这个错误

http://localhost:30303//pythonImpl/pythonServer. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

我想我必须在从python服务器发送响应时添加头。知道怎么做吗???在

谢谢。在

乌德帕特:试了很多东西,都没用。所以为了让它能正常工作,我简单地添加了chrome扩展来解决CORS问题。可以找到扩展名here。不过,我欢迎在代码本身中解决这个问题的任何想法。在


Tags: 代码服务器clientloglocalhosthttpnewserver